2015-09-18 11:55:31 +00:00
|
|
|
// Copyright (c) 2011-2015 The Cryptonote developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
#include <System/Dispatcher.h>
|
2015-07-30 15:22:07 +00:00
|
|
|
#include <System/ContextGroup.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <System/Event.h>
|
|
|
|
#include <System/InterruptedException.h>
|
|
|
|
#include <System/Ipv4Address.h>
|
|
|
|
#include <System/TcpConnection.h>
|
|
|
|
#include <System/TcpConnector.h>
|
|
|
|
#include <System/TcpListener.h>
|
|
|
|
#include <System/Timer.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
using namespace System;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
class TcpListenerTests : public testing::Test {
|
|
|
|
public:
|
|
|
|
TcpListenerTests() :
|
|
|
|
event(dispatcher), listener(dispatcher, Ipv4Address("127.0.0.1"), 6666), contextGroup(dispatcher) {
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
Dispatcher dispatcher;
|
2015-07-30 15:22:07 +00:00
|
|
|
Event event;
|
|
|
|
TcpListener listener;
|
|
|
|
ContextGroup contextGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TcpListenerTests, tcpListener1) {
|
|
|
|
contextGroup.spawn([&] {
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpConnector connector(dispatcher);
|
|
|
|
connector.connect(Ipv4Address("127.0.0.1"), 6666);
|
|
|
|
event.set();
|
|
|
|
});
|
|
|
|
|
|
|
|
listener.accept();
|
|
|
|
event.wait();
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
|
|
|
|
TEST_F(TcpListenerTests, interruptListener) {
|
2015-05-27 12:08:46 +00:00
|
|
|
bool stopped = false;
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&] {
|
|
|
|
try {
|
|
|
|
listener.accept();
|
|
|
|
} catch (InterruptedException&) {
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contextGroup.interrupt();
|
|
|
|
contextGroup.wait();
|
2015-05-27 12:08:46 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
ASSERT_TRUE(stopped);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TcpListenerTests, acceptAfterInterrupt) {
|
|
|
|
bool stopped = false;
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
try {
|
|
|
|
listener.accept();
|
|
|
|
} catch (InterruptedException&) {
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contextGroup.interrupt();
|
|
|
|
contextGroup.wait();
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(stopped);
|
2015-07-30 15:22:07 +00:00
|
|
|
stopped = false;
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
Timer(dispatcher).sleep(std::chrono::milliseconds(1));
|
|
|
|
contextGroup.interrupt();
|
|
|
|
});
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
try {
|
|
|
|
TcpConnector connector(dispatcher);
|
|
|
|
connector.connect(Ipv4Address("127.0.0.1"), 6666);
|
|
|
|
} catch (InterruptedException&) {
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
try {
|
|
|
|
listener.accept();
|
|
|
|
} catch (InterruptedException&) {
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contextGroup.wait();
|
|
|
|
ASSERT_FALSE(stopped);
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TEST_F(TcpListenerTests, tcpListener3) {
|
2015-05-27 12:08:46 +00:00
|
|
|
bool stopped = false;
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&] {
|
|
|
|
Timer(dispatcher).sleep(std::chrono::milliseconds(100));
|
|
|
|
contextGroup.interrupt();
|
2015-05-27 12:08:46 +00:00
|
|
|
});
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&] {
|
|
|
|
try {
|
|
|
|
listener.accept();
|
|
|
|
} catch (InterruptedException&) {
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
});
|
2015-05-27 12:08:46 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.wait();
|
2015-05-27 12:08:46 +00:00
|
|
|
ASSERT_TRUE(stopped);
|
|
|
|
}
|