|
| 1 | +package com.baeldung.socketchannel; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.io.ObjectInputStream; |
| 7 | +import java.io.ObjectOutputStream; |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.nio.channels.Channels; |
| 10 | +import java.nio.channels.ServerSocketChannel; |
| 11 | +import java.nio.channels.SocketChannel; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | +import java.util.concurrent.Future; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +public class SocketChannelUnitTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void givenServerStarted_whenClientConnects_thenConnectionIsSuccessful() throws Exception { |
| 23 | + try (ServerSocketChannel server = ServerSocketChannel.open() |
| 24 | + .bind(new InetSocketAddress(6000))) { |
| 25 | + int port = ((InetSocketAddress) server.getLocalAddress()).getPort(); |
| 26 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 27 | + Future<Boolean> future = executor.submit(() -> { |
| 28 | + try (SocketChannel client = server.accept()) { |
| 29 | + return client.isConnected(); |
| 30 | + } |
| 31 | + }); |
| 32 | + try (SocketChannel client = SocketChannel.open()) { |
| 33 | + client.configureBlocking(true); |
| 34 | + client.connect(new InetSocketAddress("localhost", 6000)); |
| 35 | + while (!client.finishConnect()) { |
| 36 | + Thread.sleep(10); |
| 37 | + } |
| 38 | + } |
| 39 | + assertTrue(future.get(2, TimeUnit.SECONDS)); |
| 40 | + executor.shutdown(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void givenClientSendsObject_whenServerReceives_thenDataMatches() throws Exception { |
| 46 | + try (ServerSocketChannel server = ServerSocketChannel.open() |
| 47 | + .bind(new InetSocketAddress(6000))) { |
| 48 | + int port = ((InetSocketAddress) server.getLocalAddress()).getPort(); |
| 49 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 50 | + Future<MyObject> future = executor.submit(() -> { |
| 51 | + try (SocketChannel client = server.accept(); ObjectInputStream objIn = new ObjectInputStream(Channels.newInputStream(client))) { |
| 52 | + return (MyObject) objIn.readObject(); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + try (SocketChannel client = SocketChannel.open()) { |
| 57 | + client.configureBlocking(true); |
| 58 | + client.connect(new InetSocketAddress("localhost", 6000)); |
| 59 | + |
| 60 | + // Ensure connection is fully established before writing |
| 61 | + while (!client.finishConnect()) { |
| 62 | + Thread.sleep(10); |
| 63 | + } |
| 64 | + |
| 65 | + try (ObjectOutputStream objOut = new ObjectOutputStream(Channels.newOutputStream(client))) { |
| 66 | + objOut.writeObject(new MyObject("Test User", 25)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + MyObject received = future.get(2, TimeUnit.SECONDS); |
| 71 | + assertEquals("Test User", received.getName()); |
| 72 | + assertEquals(25, received.getAge()); |
| 73 | + executor.shutdown(); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments