From c9d1a90fb971949b90e06ea56a32b72e36aeec8a Mon Sep 17 00:00:00 2001 From: Adam Waldenberg Date: Fri, 3 Feb 2023 18:52:15 +0100 Subject: [PATCH] Add preliminary PublishPeersChannelHandlerTest (currently disabled) --- .../PublishPeersChannelHandlerTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 application/src/test/java/org/unigrid/hedgehog/model/network/handler/PublishPeersChannelHandlerTest.java diff --git a/application/src/test/java/org/unigrid/hedgehog/model/network/handler/PublishPeersChannelHandlerTest.java b/application/src/test/java/org/unigrid/hedgehog/model/network/handler/PublishPeersChannelHandlerTest.java new file mode 100644 index 00000000..739a3f35 --- /dev/null +++ b/application/src/test/java/org/unigrid/hedgehog/model/network/handler/PublishPeersChannelHandlerTest.java @@ -0,0 +1,89 @@ +/* + Unigrid Hedgehog + Copyright © 2021-2023 The Unigrid Foundation, UGD Software AB + + This program is free software: you can redistribute it and/or modify it under the terms of the + addended GNU Affero General Public License as published by the The Unigrid Foundation and + the Free Software Foundation, version 3 of the License (see COPYING and COPYING.addendum). + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License and the addendum for more details. + + You should have received an addended copy of the GNU Affero General Public License with this program. + If not, see and . + */ + +package org.unigrid.hedgehog.model.network.handler; + +import io.netty.channel.ChannelHandlerContext; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import lombok.SneakyThrows; +import mockit.Mocked; +import mockit.Tested; +import net.jqwik.api.Example; +import net.jqwik.api.ForAll; +import net.jqwik.api.constraints.ByteRange; +import net.jqwik.api.Property; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import org.unigrid.hedgehog.client.P2PClient; +import org.unigrid.hedgehog.model.network.packet.Ping; +import org.unigrid.hedgehog.model.network.initializer.RegisterQuicChannelInitializer; +import org.unigrid.hedgehog.model.network.packet.PublishPeers; +import org.unigrid.hedgehog.model.network.schedule.PingSchedule; +import org.unigrid.hedgehog.server.TestServer; + +public class PublishPeersChannelHandler extends BaseHandlerTest { + public PublishPeersChannelHandler() { + super(PublishPeersChannelHandler.class); + } + + //@Property(tries = 50) + public void shoulBeAbleToPingNetwork(@ForAll("provideTestServers") List servers, + @ForAll @ByteRange(min = 3, max = 5) byte pingsPerServer, + @Mocked PingSchedule pingSchedule) throws Exception { + + final AtomicInteger invocations = new AtomicInteger(); + int expectedInvocations = 0; + + setChannelCallback(Optional.of((ctx, ping) -> { + /* Only count triggers on the server-side */ + if (RegisterQuicChannelInitializer.Type.SERVER.is(ctx.channel())) { + invocations.incrementAndGet(); + } + })); + + for (TestServer server : servers) { + final String host = server.getP2p().getHostName(); + final int port = server.getP2p().getPort(); + final P2PClient client = new P2PClient(host, port); + + for (int i = 0; i < pingsPerServer; i++) { + client.send(Ping.builder().build()); + expectedInvocations++; + } + + await().untilAtomic(invocations, is(expectedInvocations)); + client.closeDirty(); + } + + await().untilAtomic(invocations, is(expectedInvocations)); + } + + @Example + @SneakyThrows + public void shouldSetResponseFlagOnResponse(@Mocked ChannelHandlerContext context, + @Tested PublishPeersChannelHandler handler) { + + /*final Ping ping = PublishPeers.builder(). + assertThat(ping.getNanoTime(), not(0)); + assertThat(ping.isResponse(), is(false)); + + handler.typedChannelRead(context, ping); + assertThat(ping.isResponse(), is(true));*/ + } +}