diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-10-16 00:37:21 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 22:56:48 +0200 |
commit | c47870421f189eb98fc66e912693d73fbd8477ee (patch) | |
tree | 036ead590fa17bef279de483489a880c54ef4ba1 /tests | |
parent | 0c1b3db7c4d9a4bcde4160c437613b32cd4081d6 (diff) |
Reuse peer connection if subscribed to same key
Diffstat (limited to 'tests')
-rw-r--r-- | tests/main.cpp | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/tests/main.cpp b/tests/main.cpp index b2343af..d650128 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,7 +1,43 @@ -#include "../include/sibs/DirectConnection.hpp" +#include "../include/sibs/BootstrapConnection.hpp" +#include "../include/sibs/BootstrapNode.hpp" +#include <thread> +#include <stdio.h> +#include <string.h> + +const int PORT = 35231; + +#define REQUIRE(expr) do { if(!(expr)) { fprintf(stderr, "Assert failed: %s\n", #expr); exit(1); } }while(0) int main() { - sibs::DirectConnections user1(27137); + const sibs::PubsubKey key("abcdefghijklmnopqrstuvxyz0123456789", 35); + sibs::BootstrapNode boostrapNode(sibs::Ipv4(nullptr, PORT)); + + sibs::BootstrapConnection connection1(sibs::Ipv4("127.0.0.1", PORT)); + bool gotData1 = false; + connection1.listen(key, [&gotData1](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size) + { + if(size == 5 && strncmp((const char*)data, "hello", 5) == 0) + gotData1 = true; + return true; + }); + + sibs::BootstrapConnection connection2(sibs::Ipv4("127.0.0.1", PORT)); + bool gotData2 = false; + connection2.listen(key, [&gotData2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size) + { + if(size == 5 && strncmp((const char*)data, "hello", 5) == 0) + gotData2 = true; + return true; + }); + // wait until connection1 and connection2 receive each other as peers from bootstrap node + std::this_thread::sleep_for(std::chrono::seconds(3)); + + connection1.put(key, "hello", 5); + std::this_thread::sleep_for(std::chrono::seconds(3)); + + REQUIRE(gotData1); + REQUIRE(gotData2); + return 0; } |