mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-03 04:19:12 +00:00
Making testcase more resilient to FileSystem behavior
This commit is contained in:
parent
80f46432da
commit
292d35dafd
@ -18,6 +18,9 @@
|
|||||||
|
|
||||||
package org.eclipse.jetty.security;
|
package org.eclipse.jetty.security;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
@ -25,17 +28,65 @@ import java.io.Writer;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import org.eclipse.jetty.toolchain.test.FS;
|
import org.eclipse.jetty.toolchain.test.FS;
|
||||||
import org.eclipse.jetty.toolchain.test.TestingDir;
|
import org.eclipse.jetty.toolchain.test.TestingDir;
|
||||||
import org.eclipse.jetty.util.security.Credential;
|
import org.eclipse.jetty.util.security.Credential;
|
||||||
import org.junit.Assert;
|
import org.hamcrest.Matcher;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class PropertyUserStoreTest
|
public class PropertyUserStoreTest
|
||||||
{
|
{
|
||||||
|
private final class UserCount implements PropertyUserStore.UserListener
|
||||||
|
{
|
||||||
|
private final AtomicInteger userCount = new AtomicInteger();
|
||||||
|
private final List<String> users = new ArrayList<String>();
|
||||||
|
|
||||||
|
private UserCount()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(String username, Credential credential, String[] roleArray)
|
||||||
|
{
|
||||||
|
if (!users.contains(username))
|
||||||
|
{
|
||||||
|
users.add(username);
|
||||||
|
userCount.getAndIncrement();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(String username)
|
||||||
|
{
|
||||||
|
users.remove(username);
|
||||||
|
userCount.getAndDecrement();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void awaitCount(int expectedCount) throws InterruptedException
|
||||||
|
{
|
||||||
|
long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);
|
||||||
|
|
||||||
|
while (userCount.get() != expectedCount && (System.currentTimeMillis() < timeout))
|
||||||
|
{
|
||||||
|
TimeUnit.MILLISECONDS.sleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThatCount(is(expectedCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void assertThatCount(Matcher<Integer> matcher)
|
||||||
|
{
|
||||||
|
assertThat("User count",userCount.get(),matcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void assertThatUsers(Matcher<Iterable<? super String>> matcher)
|
||||||
|
{
|
||||||
|
assertThat("Users list",users,matcher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public TestingDir testdir = new TestingDir();
|
public TestingDir testdir = new TestingDir();
|
||||||
|
|
||||||
@ -67,88 +118,57 @@ public class PropertyUserStoreTest
|
|||||||
@Test
|
@Test
|
||||||
public void testPropertyUserStoreLoad() throws Exception
|
public void testPropertyUserStoreLoad() throws Exception
|
||||||
{
|
{
|
||||||
final AtomicInteger userCount = new AtomicInteger();
|
final UserCount userCount = new UserCount();
|
||||||
final File usersFile = initUsersText();
|
final File usersFile = initUsersText();
|
||||||
|
|
||||||
PropertyUserStore store = new PropertyUserStore();
|
PropertyUserStore store = new PropertyUserStore();
|
||||||
store.setConfigPath(usersFile);
|
store.setConfigPath(usersFile);
|
||||||
|
|
||||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
store.registerUserListener(userCount);
|
||||||
{
|
|
||||||
public void update(String username, Credential credential, String[] roleArray)
|
|
||||||
{
|
|
||||||
userCount.getAndIncrement();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(String username)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
store.start();
|
store.start();
|
||||||
|
|
||||||
Assert.assertNotNull("Failed to retrieve UserIdentity directly from PropertyUserStore", store.getUserIdentity("tom"));
|
assertThat("Failed to retrieve UserIdentity directly from PropertyUserStore", store.getUserIdentity("tom"), notNullValue());
|
||||||
Assert.assertNotNull("Failed to retrieve UserIdentity directly from PropertyUserStore", store.getUserIdentity("dick"));
|
assertThat("Failed to retrieve UserIdentity directly from PropertyUserStore", store.getUserIdentity("dick"), notNullValue());
|
||||||
Assert.assertNotNull("Failed to retrieve UserIdentity directly from PropertyUserStore", store.getUserIdentity("harry"));
|
assertThat("Failed to retrieve UserIdentity directly from PropertyUserStore", store.getUserIdentity("harry"), notNullValue());
|
||||||
Assert.assertEquals(3,userCount.get());
|
userCount.assertThatCount(is(3));
|
||||||
|
userCount.awaitCount(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPropertyUserStoreLoadUpdateUser() throws Exception
|
public void testPropertyUserStoreLoadUpdateUser() throws Exception
|
||||||
{
|
{
|
||||||
final AtomicInteger userCount = new AtomicInteger();
|
final UserCount userCount = new UserCount();
|
||||||
final List<String> users = new ArrayList<String>();
|
|
||||||
final File usersFile = initUsersText();
|
final File usersFile = initUsersText();
|
||||||
|
|
||||||
PropertyUserStore store = new PropertyUserStore();
|
PropertyUserStore store = new PropertyUserStore();
|
||||||
store.setHotReload(true);
|
store.setHotReload(true);
|
||||||
store.setConfigPath(usersFile);
|
store.setConfigPath(usersFile);
|
||||||
|
|
||||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
store.registerUserListener(userCount);
|
||||||
{
|
|
||||||
public void update(String username, Credential credential, String[] roleArray)
|
|
||||||
{
|
|
||||||
if (!users.contains(username))
|
|
||||||
{
|
|
||||||
users.add(username);
|
|
||||||
userCount.getAndIncrement();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(String username)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
store.start();
|
store.start();
|
||||||
|
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
|
|
||||||
Assert.assertEquals(3,userCount.get());
|
userCount.assertThatCount(is(3));
|
||||||
|
|
||||||
addAdditionalUser(usersFile,"skip: skip, roleA\n");
|
addAdditionalUser(usersFile,"skip: skip, roleA\n");
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
userCount.awaitCount(4);
|
||||||
while (userCount.get() < 4 && (System.currentTimeMillis() - start) < 10000)
|
|
||||||
{
|
|
||||||
Thread.sleep(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.assertNotNull("Failed to retrieve UserIdentity from PropertyUserStore directly", store.getUserIdentity("skip"));
|
assertThat("Failed to retrieve UserIdentity from PropertyUserStore directly", store.getUserIdentity("skip"), notNullValue());
|
||||||
Assert.assertEquals(4,userCount.get());
|
|
||||||
|
|
||||||
Assert.assertTrue(users.contains("skip"));
|
userCount.assertThatCount(is(4));
|
||||||
|
userCount.assertThatUsers(hasItem("skip"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPropertyUserStoreLoadRemoveUser() throws Exception
|
public void testPropertyUserStoreLoadRemoveUser() throws Exception
|
||||||
{
|
{
|
||||||
|
final UserCount userCount = new UserCount();
|
||||||
// initial user file (3) users
|
// initial user file (3) users
|
||||||
final File usersFile = initUsersText();
|
final File usersFile = initUsersText();
|
||||||
final AtomicInteger userCount = new AtomicInteger();
|
|
||||||
final List<String> users = new ArrayList<String>();
|
|
||||||
|
|
||||||
// adding 4th user
|
// adding 4th user
|
||||||
addAdditionalUser(usersFile,"skip: skip, roleA\n");
|
addAdditionalUser(usersFile,"skip: skip, roleA\n");
|
||||||
@ -157,33 +177,17 @@ public class PropertyUserStoreTest
|
|||||||
store.setHotReload(true);
|
store.setHotReload(true);
|
||||||
store.setConfigPath(usersFile);
|
store.setConfigPath(usersFile);
|
||||||
|
|
||||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
store.registerUserListener(userCount);
|
||||||
{
|
|
||||||
public void update(String username, Credential credential, String[] roleArray)
|
|
||||||
{
|
|
||||||
if (!users.contains(username))
|
|
||||||
{
|
|
||||||
users.add(username);
|
|
||||||
userCount.getAndIncrement();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(String username)
|
|
||||||
{
|
|
||||||
users.remove(username);
|
|
||||||
userCount.getAndDecrement();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
store.start();
|
store.start();
|
||||||
|
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
|
|
||||||
Assert.assertEquals(4,userCount.get());
|
userCount.assertThatCount(is(4));
|
||||||
|
|
||||||
// rewrite file with original 3 users
|
// rewrite file with original 3 users
|
||||||
initUsersText();
|
initUsersText();
|
||||||
Thread.sleep(3000);
|
|
||||||
Assert.assertEquals(3,userCount.get());
|
userCount.awaitCount(3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user