package com.cloudera.flume.agent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.cloudera.flume.conf.Context;
import com.cloudera.flume.conf.FlumeBuilder;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.flume.conf.FlumeSpecException;
import com.cloudera.flume.conf.SinkFactoryImpl;
import com.cloudera.flume.conf.SinkFactory.SinkBuilder;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.master.FlumeMaster;
import com.cloudera.util.NetUtils;
FlumeMaster master = null;
FlumeConfiguration cfg;
@Before
public void setCfg()
throws IOException {
cfg = FlumeConfiguration.createTestableConfiguration();
cfg.set(FlumeConfiguration.MASTER_STORE, "memory");
cfg.set(FlumeConfiguration.WEBAPPS_PATH, "build/webapps");
}
@After
if (master != null) {
master.shutdown();
master = null;
}
}
@Test
public void testNoHang()
throws IOException, InterruptedException,
FlumeSpecException {
SinkFactoryImpl sf = new SinkFactoryImpl();
sf.setSink("hang", new SinkBuilder() {
@Override
public EventSink
build(Context context, String... argv) {
return new EventSink.Base() {
@Override
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
});
FlumeBuilder.setSinkFactory(sf);
final FlumeMaster master = new FlumeMaster(cfg);
MasterRPC rpc = new DirectMasterRPC(master);
final FlumeNode node = new FlumeNode(rpc, false, false);
assertEquals(0, node.getLogicalNodeManager().getNodes().size());
final CountDownLatch done = new CountDownLatch(1);
new Thread() {
LivenessManager liveMan = node.getLivenessManager();
try {
master.getSpecMan().setConfig(NetUtils.localhost(), "flow",
"asciisynth(0)", "hang");
liveMan.heartbeatChecks();
Thread.sleep(250);
master.getSpecMan().setConfig(NetUtils.localhost(), "flow",
"asciisynth(0)", "hang");
liveMan.heartbeatChecks();
Thread.sleep(250);
master.getSpecMan().setConfig(NetUtils.localhost(), "flow",
"asciisynth(0)", "hang");
liveMan.heartbeatChecks();
Thread.sleep(250);
} catch (IOException e) {
return;
} catch (FlumeSpecException e) {
return;
} catch (InterruptedException e) {
return;
}
done.countDown();
}
}.start();
assertTrue("close call hung the heartbeat", done.await(2000,
TimeUnit.MILLISECONDS));
}
}