@Test
public void testSimpleClient() throws TException, InterruptedException,
TransactionIdTooOldException, NotConnectedToServerException,
WatchAlreadyPlacedException, IOException {
ServerCore notifier = null;
try {
String editPath = conf.get(AvatarNode.DFS_SHARED_EDITS_DIR0_KEY);
LOG.info("edit path: " + editPath);
File file = new File(editPath);
String[] names = file.list();
for (String name : names) {
LOG.info("content: " + name);
}
conf.setLong(NotifierConfig.LOG_READER_STREAM_TIMEOUT, 100);
notifier = ServerCore.createNotifier(conf, nameService);
int port = conf.getInt("notifier.thrift.port", -1);
int clientPort = MiniDFSCluster.getFreePort();
LOG.info("port: " + port);
MyClientWatcher myWatcher = new MyClientWatcher();
NamespaceNotifierClient myClient = new NamespaceNotifierClient(myWatcher,
localhostAddr, port, clientPort);
Thread clientThread = new Thread(myClient);
clientThread.start();
// wait for connect
long start = System.currentTimeMillis();
while (!myWatcher.connected &&
System.currentTimeMillis() < start + 2000) {
Thread.sleep(500);
}
myClient.placeWatch("/", EventType.FILE_ADDED, -1);
myClient.placeWatch("/", EventType.FILE_CLOSED, -1);
myClient.placeWatch("/", EventType.NODE_DELETED, -1);
int numRequests = random.nextInt(50) + 1;
// issue number of requests.
for (int i = 0; i < numRequests; i++) {
FSDataOutputStream out = fileSys.create(new Path("/testfile_" + i));
out.close();
}
LOG.info("Start failover");
cluster.failOver();
cluster.restartStandby();
Thread.sleep(100);
for (int i = 0; i < numRequests; i++) {
fileSys.delete(new Path("/testfile_" + i), false);
}
start = System.currentTimeMillis();
while (myWatcher.numDeleted < numRequests &&
System.currentTimeMillis() < start + 6000) {
Thread.sleep(500);
}
assertEquals(numRequests, myWatcher.numAdd);
assertEquals(numRequests, myWatcher.numClosed);
assertEquals(numRequests, myWatcher.numDeleted);
// check the events from sub directors.
myWatcher.resetCounters();
// issue number of requests.
for (int i = 0; i < numRequests; i++) {
FSDataOutputStream out = fileSys.create(new Path("/user/dikang/testfile_" + i));
out.close();
}
LOG.info("Start failover");
cluster.failOver();
cluster.restartStandby();
Thread.sleep(100);
for (int i = 0; i < numRequests; i++) {
fileSys.delete(new Path("/user/dikang/testfile_" + i), false);
}
start = System.currentTimeMillis();
while (myWatcher.numDeleted < numRequests &&
System.currentTimeMillis() < start + 6000) {
Thread.sleep(500);
}
assertEquals(numRequests, myWatcher.numAdd);
assertEquals(numRequests, myWatcher.numClosed);
assertEquals(numRequests, myWatcher.numDeleted);
} finally {
if (notifier != null) {
notifier.shutdown();
notifier.join();
}
}
}