From b19246e8314361904ae73b20d45e44b85353142d Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 23 Jan 2018 16:24:14 -0500 Subject: [PATCH] Fix TcpClientServerDemoTest race condition https://build.spring.io/browse/INTSAMPLES-NIGHTLY-2342 The `TcpClientServerDemoTest.testMultiPlex()` relies on the `HashSet.size()` for assertion meanwhile that property is not `volatile`. Since we change the state of the `HashSet` from other threads the non-`volatile` property is not reliable source of information. * Change the test-case to use `LinkedBlockingQueue` instead of `HashSet` which uses `AtomicInteger` to track its `size` --- .../TcpClientServerDemoTest.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java b/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java index 669df2f8..4b14445a 100644 --- a/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java +++ b/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,9 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.util.HashSet; -import java.util.Set; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import org.junit.Before; @@ -51,6 +51,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * Requires correlation data in the payload. * * @author Gary Russell + * @author Artem Bilan + * * @since 2.1 * */ @@ -81,19 +83,15 @@ public class TcpClientServerDemoTest { public void testMultiPlex() throws Exception { TaskExecutor executor = new SimpleAsyncTaskExecutor(); final CountDownLatch latch = new CountDownLatch(100); - final Set results = new HashSet(); + final BlockingQueue results = new LinkedBlockingQueue<>(); for (int i = 100; i < 200; i++) { results.add(i); final int j = i; - executor.execute(new Runnable() { - - @Override - public void run() { - String result = gw.send(j + "Hello world!"); // first 3 bytes is correlationid - assertEquals(j + "Hello world!:echo", result); - results.remove(j); - latch.countDown(); - } + executor.execute(() -> { + String result = gw.send(j + "Hello world!"); // first 3 bytes is correlationid + assertEquals(j + "Hello world!:echo", result); + results.remove(j); + latch.countDown(); }); } assertTrue(latch.await(60, TimeUnit.SECONDS));