Skip to content

Commit

Permalink
Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sboikov committed May 5, 2016
1 parent 764c97b commit b646b54
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 92 deletions.
Expand Up @@ -341,7 +341,7 @@ public void testNonNullTopicWithDeployment() throws Exception {
public void testListenActor() throws Exception { public void testListenActor() throws Exception {
latch = new CountDownLatch(MSG_CNT * (GRID_CNT + 1)); latch = new CountDownLatch(MSG_CNT * (GRID_CNT + 1));


grid(0).message().remoteListen(null, new Actor(grid(0).cluster())); grid(0).message().remoteListen(null, new Actor(grid(0).localNode().id()));


try { try {
Ignite g = startGrid("anotherGrid"); Ignite g = startGrid("anotherGrid");
Expand Down Expand Up @@ -376,7 +376,7 @@ public void testListenActor() throws Exception {
private void listen(final ClusterGroup prj, @Nullable Object topic, final boolean ret) throws Exception { private void listen(final ClusterGroup prj, @Nullable Object topic, final boolean ret) throws Exception {
assert prj != null; assert prj != null;


message(prj).remoteListen(topic, new Listener(prj, ret)); message(prj).remoteListen(topic, new Listener(grid(0).localNode().id(), ret));
} }


/** /**
Expand Down Expand Up @@ -428,23 +428,21 @@ private void checkNodes(List<UUID> expNodes) {


/** */ /** */
private static class Listener implements P2<UUID, Object> { private static class Listener implements P2<UUID, Object> {
/** */ /** Source node id. */
private final ClusterGroup prj; private final UUID sourceNodeId;

/** */ /** */
private final boolean ret; private final boolean ret;

/** */ /** */
@IgniteInstanceResource @IgniteInstanceResource
private Ignite ignite; private Ignite ignite;


/** /**
* @param prj Projection.
* @param ret Return value. * @param ret Return value.
* @param sourceNodeId expected source node id
*/ */
private Listener(ClusterGroup prj, boolean ret) { private Listener(UUID sourceNodeId, boolean ret) {
this.prj = prj;
this.ret = ret; this.ret = ret;
this.sourceNodeId = sourceNodeId;
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand All @@ -454,7 +452,7 @@ private Listener(ClusterGroup prj, boolean ret) {


X.println("Received message [nodeId=" + nodeId + ", locNodeId=" + ignite.cluster().localNode().id() + ']'); X.println("Received message [nodeId=" + nodeId + ", locNodeId=" + ignite.cluster().localNode().id() + ']');


assertEquals(prj.ignite().cluster().localNode().id(), nodeId); assertEquals(sourceNodeId, nodeId);
assertEquals(MSG, msg); assertEquals(MSG, msg);


nodes.add(ignite.configuration().getNodeId()); nodes.add(ignite.configuration().getNodeId());
Expand All @@ -467,14 +465,14 @@ private Listener(ClusterGroup prj, boolean ret) {


/** */ /** */
private static class Actor extends MessagingListenActor<Object> { private static class Actor extends MessagingListenActor<Object> {
/** */ /** Source node id. */
private final ClusterGroup prj; private final UUID sourceNodeId;


/** /**
* @param prj Projection. * @param sourceNodeId expected source node id
*/ */
private Actor(ClusterGroup prj) { private Actor(UUID sourceNodeId) {
this.prj = prj; this.sourceNodeId = sourceNodeId;
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand All @@ -485,7 +483,7 @@ private Actor(ClusterGroup prj) {


X.println("Received message [nodeId=" + nodeId + ", locNodeId=" + locNodeId + ']'); X.println("Received message [nodeId=" + nodeId + ", locNodeId=" + locNodeId + ']');


assertEquals(prj.ignite().cluster().localNode().id(), nodeId); assertEquals(sourceNodeId, nodeId);
assertEquals(MSG, msg); assertEquals(MSG, msg);


nodes.add(locNodeId); nodes.add(locNodeId);
Expand Down
Expand Up @@ -17,6 +17,8 @@


package org.apache.ignite.internal.util.io; package org.apache.ignite.internal.util.io;


import org.apache.ignite.internal.util.lang.GridAbsPredicate;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;


import static org.apache.ignite.IgniteSystemProperties.IGNITE_MARSHAL_BUFFERS_RECHECK; import static org.apache.ignite.IgniteSystemProperties.IGNITE_MARSHAL_BUFFERS_RECHECK;
Expand All @@ -31,48 +33,31 @@ public class GridUnsafeDataOutputArraySizingSelfTest extends GridCommonAbstractT
/** Big array. */ /** Big array. */
private static final byte[] BIG = new byte[2048]; private static final byte[] BIG = new byte[2048];


/** Buffer timeout. */
private static final long BUFFER_TIMEOUT = 1000;

/** Wait timeout is bigger then buffer timeout to prevent failures due to time measurement error. */
private static final long WAIT_BUFFER_TIMEOUT = BUFFER_TIMEOUT + BUFFER_TIMEOUT / 2;

/**
*
*/
static { static {
System.setProperty(IGNITE_MARSHAL_BUFFERS_RECHECK, "1000"); System.setProperty(IGNITE_MARSHAL_BUFFERS_RECHECK, Long.toString(BUFFER_TIMEOUT));
} }


/** /**
* @throws Exception If failed. * @throws Exception If failed.
*/ */
@SuppressWarnings("BusyWait") @SuppressWarnings("BusyWait")
public void testSmall() throws Exception { public void testSmall() throws Exception {
GridUnsafeDataOutput out = new GridUnsafeDataOutput(512); final GridUnsafeDataOutput out = new GridUnsafeDataOutput(512);

for (int i = 0; i < 11; i++) {
Thread.sleep(100);

writeSmall(out);
}

assertEquals(256, out.internalArray().length);

for (int i = 0; i < 11; i++) {
Thread.sleep(100);

writeSmall(out);
}


assertEquals(128, out.internalArray().length); assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 256), WAIT_BUFFER_TIMEOUT));

assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 128), WAIT_BUFFER_TIMEOUT));
for (int i = 0; i < 11; i++) { assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 64), WAIT_BUFFER_TIMEOUT));
Thread.sleep(100); assertFalse(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 32), WAIT_BUFFER_TIMEOUT));

assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 64), WAIT_BUFFER_TIMEOUT));
writeSmall(out);
}

assertEquals(64, out.internalArray().length);

for (int i = 0; i < 11; i++) {
Thread.sleep(100);

writeSmall(out);
}

assertEquals(64, out.internalArray().length);
} }


/** /**
Expand All @@ -81,7 +66,8 @@ public void testSmall() throws Exception {
public void testBig() throws Exception { public void testBig() throws Exception {
GridUnsafeDataOutput out = new GridUnsafeDataOutput(512); GridUnsafeDataOutput out = new GridUnsafeDataOutput(512);


writeBig(out); out.write(BIG);
out.reset();


assertEquals(4096, out.internalArray().length); assertEquals(4096, out.internalArray().length);
} }
Expand All @@ -96,8 +82,10 @@ public void testChanged1() throws Exception {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Thread.sleep(100); Thread.sleep(100);


writeSmall(out); out.write(SMALL);
writeBig(out); out.reset();
out.write(BIG);
out.reset();
} }


assertEquals(4096, out.internalArray().length); assertEquals(4096, out.internalArray().length);
Expand All @@ -108,54 +96,58 @@ public void testChanged1() throws Exception {
*/ */
@SuppressWarnings("BusyWait") @SuppressWarnings("BusyWait")
public void testChanged2() throws Exception { public void testChanged2() throws Exception {
GridUnsafeDataOutput out = new GridUnsafeDataOutput(512); final GridUnsafeDataOutput out = new GridUnsafeDataOutput(512);

for (int i = 0; i < 11; i++) {
Thread.sleep(100);

writeSmall(out);
}

assertEquals(256, out.internalArray().length);

writeBig(out);

assertEquals(4096, out.internalArray().length);


for (int i = 0; i < 11; i++) { assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 256), WAIT_BUFFER_TIMEOUT));
Thread.sleep(100);

writeSmall(out);
}


out.write(BIG);
out.reset();
assertEquals(4096, out.internalArray().length); assertEquals(4096, out.internalArray().length);


for (int i = 0; i < 11; i++) { assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 4096), WAIT_BUFFER_TIMEOUT));
Thread.sleep(100); assertTrue(GridTestUtils.waitForCondition(new WriteAndCheckPredicate(out, SMALL, 2048), 2 * WAIT_BUFFER_TIMEOUT));

writeSmall(out);
}

assertEquals(2048, out.internalArray().length);
}

/**
* @param out Output.
* @throws Exception If failed.
*/
private void writeSmall(GridUnsafeDataOutput out) throws Exception {
out.write(SMALL);

out.reset();
} }


/** /**
* @param out Output. *
* @throws Exception If failed.
*/ */
private void writeBig(GridUnsafeDataOutput out) throws Exception { private static class WriteAndCheckPredicate implements GridAbsPredicate {
out.write(BIG); /** */
final GridUnsafeDataOutput out;

/** */
final byte [] bytes;

/** */
final int len;

/**
* @param out Out.
* @param bytes Bytes.
* @param len Length.
*/
WriteAndCheckPredicate(GridUnsafeDataOutput out, byte[] bytes, int len) {
this.out = out;
this.bytes = bytes;
this.len = len;
}


out.reset(); /**
* {@inheritDoc}
*/
@Override public boolean apply() {
try {
out.write(bytes);
out.reset();

System.out.println("L=" + out.internalArray().length);

return out.internalArray().length == len;
}
catch (Exception e) {
assertTrue("Unexpected exception: " + e.getMessage(), false);
return false;
}
}
} }
} }

0 comments on commit b646b54

Please sign in to comment.