diff options
Diffstat (limited to 'test/connectionhandlertest.cc')
-rw-r--r-- | test/connectionhandlertest.cc | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/test/connectionhandlertest.cc b/test/connectionhandlertest.cc new file mode 100644 index 0000000..b35cba4 --- /dev/null +++ b/test/connectionhandlertest.cc @@ -0,0 +1,152 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * connectionhandlertest.cc + * + * Sat Jun 6 20:00:25 CEST 2020 + * Copyright 2020 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Munia. + * + * Munia is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Munia 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Munia; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "dgunit.h" + +#include <connectionhandler.h> + +class ConnectionHandlerTest + : public DGUnit +{ +public: + ConnectionHandlerTest() + { + DGUNIT_TEST(ConnectionHandlerTest::test); + } + + bool find_client(const SubscriberList& clst, clientid_t id) + { + for(const auto& client : clst) + { + if(client.first == id) + { + return true; + } + } + return false; + } + + void test() + { + ConnectionHandler &h = connection_handler; + + h.init((clientid_t)1); + h.subscribe((clientid_t)1, (nodeid_t)1); + h.subscribe((clientid_t)1, (nodeid_t)2); + + h.init((clientid_t)2); + h.subscribe((clientid_t)2, (nodeid_t)1); + h.subscribe((clientid_t)2, (nodeid_t)2); + + h.init((clientid_t)3); + h.subscribe((clientid_t)3, (nodeid_t)3); + + { + NodeIdList nodes; + nodes.push_back((nodeid_t)1); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)3)); // Got client 3? + } + + { + NodeIdList nodes; + nodes.push_back((nodeid_t)3); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(!find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(find_client(clst, (clientid_t)3)); // Got client 3? + } + + { + NodeIdList nodes; + nodes.push_back((nodeid_t)4); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(!find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)3)); // Got client 3? + } + + { + NodeIdList nodes; + nodes.push_back((nodeid_t)1); + nodes.push_back((nodeid_t)2); + nodes.push_back((nodeid_t)3); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(find_client(clst, (clientid_t)3)); // Got client 3? + } + + h.close((clientid_t)1); + { + NodeIdList nodes; + nodes.push_back((nodeid_t)1); + nodes.push_back((nodeid_t)2); + nodes.push_back((nodeid_t)3); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(!find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(find_client(clst, (clientid_t)3)); // Got client 3? + } + + h.close((clientid_t)2); + { + NodeIdList nodes; + nodes.push_back((nodeid_t)1); + nodes.push_back((nodeid_t)2); + nodes.push_back((nodeid_t)3); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(!find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(find_client(clst, (clientid_t)3)); // Got client 3? + } + + h.close((clientid_t)3); + { + NodeIdList nodes; + nodes.push_back((nodeid_t)1); + nodes.push_back((nodeid_t)2); + nodes.push_back((nodeid_t)3); + SubscriberList clst = h.subscriberlist(nodes); + + DGUNIT_ASSERT(!find_client(clst, (clientid_t)1)); // Got client 1? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)2)); // Got client 2? + DGUNIT_ASSERT(!find_client(clst, (clientid_t)3)); // Got client 3? + } + } +}; + +// Registers the fixture into the 'registry' +static ConnectionHandlerTest test; |