use of org.apache.ignite.IgniteEvents in project ignite by apache.
the class GridMarshallerAbstractTest method testEvents.
/**
* @throws Exception If failed.
*/
public void testEvents() throws Exception {
IgniteConfiguration cfg = optimize(getConfiguration("g1"));
try (Ignite g1 = G.start(cfg)) {
IgniteEvents evts = events(grid().cluster().forNode(g1.cluster().localNode()));
evts.localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event gridEvt) {
return true;
}
}, EVTS_CACHE);
grid().cache(DEFAULT_CACHE_NAME).put(1, 1);
GridMarshallerTestBean inBean = newTestBean(evts);
byte[] buf = marshal(inBean);
GridMarshallerTestBean outBean = unmarshal(buf);
assert inBean.getObjectField() != null;
assert outBean.getObjectField() != null;
assert inBean.getObjectField().getClass().equals(IgniteEventsImpl.class);
assert outBean.getObjectField().getClass().equals(IgniteEventsImpl.class);
assert inBean != outBean;
assert inBean.equals(outBean);
ClusterGroup inPrj = evts.clusterGroup();
ClusterGroup outPrj = ((IgniteEvents) outBean.getObjectField()).clusterGroup();
assert inPrj.getClass().equals(outPrj.getClass());
assert F.eqNotOrdered(inPrj.nodes(), outPrj.nodes());
outBean.checkNullResources();
}
}
use of org.apache.ignite.IgniteEvents in project ignite by apache.
the class GridEventConsumeSelfTest method testApiAsync.
/**
* @throws Exception If failed.
*/
public void testApiAsync() throws Exception {
IgniteEvents evt = grid(0).events();
try {
evt.stopRemoteListenAsync(null).get();
} catch (NullPointerException ignored) {
// No-op.
}
evt.stopRemoteListenAsync(UUID.randomUUID()).get();
UUID consumeId = null;
try {
consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {
@Override
public boolean apply(UUID uuid, DiscoveryEvent evt) {
return false;
}
}, new P1<DiscoveryEvent>() {
@Override
public boolean apply(DiscoveryEvent e) {
return false;
}
}, EVTS_DISCOVERY).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
try {
consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {
@Override
public boolean apply(UUID uuid, DiscoveryEvent evt) {
return false;
}
}, new P1<DiscoveryEvent>() {
@Override
public boolean apply(DiscoveryEvent e) {
return false;
}
}).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
try {
consumeId = evt.remoteListenAsync(new P2<UUID, Event>() {
@Override
public boolean apply(UUID uuid, Event evt) {
return false;
}
}, new P1<Event>() {
@Override
public boolean apply(Event e) {
return false;
}
}).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
}
use of org.apache.ignite.IgniteEvents in project ignite by apache.
the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.
/**
* @throws Exception If failed.
*/
public void testMultithreadedWithNodeRestart() throws Exception {
final AtomicBoolean stop = new AtomicBoolean();
final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
final Collection<UUID> started = new GridConcurrentHashSet<>();
final Collection<UUID> stopped = new GridConcurrentHashSet<>();
final Random rnd = new Random();
IgniteInternalFuture<?> starterFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < CONSUME_CNT; i++) {
int idx = rnd.nextInt(GRID_CNT);
try {
IgniteEvents evts = grid(idx).events();
UUID consumeId = evts.remoteListenAsync(new P2<UUID, Event>() {
@Override
public boolean apply(UUID uuid, Event evt) {
return true;
}
}, null, EVT_JOB_STARTED).get(3000);
started.add(consumeId);
queue.add(F.t(idx, consumeId));
} catch (ClusterTopologyException ignored) {
// No-op.
}
U.sleep(10);
}
stop.set(true);
return null;
}
}, 8, "consume-starter");
IgniteInternalFuture<?> stopperFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
if (t == null)
continue;
int idx = t.get1();
UUID consumeId = t.get2();
try {
IgniteEvents evts = grid(idx).events();
evts.stopRemoteListenAsync(consumeId).get(3000);
stopped.add(consumeId);
} catch (ClusterTopologyException ignored) {
// No-op.
}
}
return null;
}
}, 4, "consume-stopper");
IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
startGrid("anotherGrid");
stopGrid("anotherGrid");
}
return null;
}
}, 1, "node-restarter");
IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
int idx = rnd.nextInt(GRID_CNT);
try {
grid(idx).compute().runAsync(F.noop()).get(3000);
} catch (IgniteException ignored) {
// Ignore all job execution related errors.
}
}
return null;
}
}, 1, "job-runner");
starterFut.get();
stopperFut.get();
nodeRestarterFut.get();
jobRunnerFut.get();
IgniteBiTuple<Integer, UUID> t;
while ((t = queue.poll()) != null) {
int idx = t.get1();
UUID consumeId = t.get2();
grid(idx).events().stopRemoteListenAsync(consumeId).get(3000);
stopped.add(consumeId);
}
Collection<UUID> notStopped = F.lose(started, true, stopped);
assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
}
Aggregations