/* * Check that we don't invalid iterators by removing descriptors during an * on_Write callback. */ void SelectServerTest::testRemoveOthersWhenWriteable() { Descriptors read_set, write_set, delete_set; LoopbackDescriptor loopback1, loopback2, loopback3; loopback1.Init(); loopback2.Init(); loopback3.Init(); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback1)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback2)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback3)); write_set.insert(&loopback1); write_set.insert(&loopback2); write_set.insert(&loopback3); loopback1.SetOnWritable(NewCallback( this, &SelectServerTest::NullHandler)); loopback2.SetOnWritable(NewCallback( this, &SelectServerTest::RemoveAndDeleteDescriptors, read_set, write_set, delete_set)); loopback3.SetOnWritable(NewCallback( this, &SelectServerTest::NullHandler)); OLA_ASSERT_EQ(3, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); m_ss->Run(); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); }
/* * Confirm we don't leak memory when the SelectServer is destroyed without all * the descriptors being removed. */ void SelectServerTest::testShutdownWithActiveDescriptors() { LoopbackDescriptor loopback_socket; loopback_socket.Init(); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback_socket)); }
/* * Confirm we can't add the same descriptor twice. */ void SelectServerTest::testDoubleAddAndRemove() { OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); // internal socket OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); LoopbackDescriptor loopback_socket; loopback_socket.Init(); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket)); OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback_socket)); OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(1, write_descriptor_count->Get()); m_ss->RemoveReadDescriptor(&loopback_socket); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(1, write_descriptor_count->Get()); m_ss->RemoveWriteDescriptor(&loopback_socket); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); // Trying to remove a second time shouldn't crash m_ss->RemoveReadDescriptor(&loopback_socket); m_ss->RemoveWriteDescriptor(&loopback_socket); }
/* * Check that RemoveWriteDescriptor is reentrant. * Similar to the case above, but this removes & deletes the descriptor from * within the OnRead callback of the same descriptor. */ void SelectServerTest::testRemoveWriteWhenReadable() { // Ownership is transferred to the SelectServer. LoopbackDescriptor *loopback = new LoopbackDescriptor(); loopback->Init(); loopback->SetOnData(NewCallback( this, &SelectServerTest::ReadDataAndRemove, static_cast<ConnectedDescriptor*>(loopback))); loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler)); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback)); OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(1, write_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); // Send some data to make this descriptor readable. uint8_t data[] = {'a'}; loopback->Send(data, arraysize(data)); m_ss->Run(); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); }
/* * Confirm we can't add invalid descriptors to the SelectServer */ void SelectServerTest::testAddInvalidDescriptor() { OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); // internal socket OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); // Adding and removing a uninitialized socket should fail LoopbackDescriptor bad_socket; OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&bad_socket)); OLA_ASSERT_FALSE(m_ss->AddWriteDescriptor(&bad_socket)); m_ss->RemoveReadDescriptor(&bad_socket); m_ss->RemoveWriteDescriptor(&bad_socket); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); // internal socket OLA_ASSERT_EQ(0, read_descriptor_count->Get()); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); }
/* * Test the interaction between read and write descriptor. */ void SelectServerTest::testReadWriteInteraction() { UnixSocket socket; socket.Init(); socket.SetOnClose(NewSingleCallback(this, &SelectServerTest::Terminate)); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&socket)); m_ss->RemoveWriteDescriptor(&socket); // now the Write end closes auto_ptr<UnixSocket> other_end(socket.OppositeEnd()); other_end->CloseClient(); m_ss->RegisterSingleTimeout( 100, ola::NewSingleCallback(this, &SelectServerTest::FatalTimeout)); m_ss->Run(); m_ss->RemoveReadDescriptor(&socket); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); }
/* * Check that RemoveWriteDescriptor is reentrant. * We use the Execute() method to close a write descriptor during the same * cycle in which it becomes writeable. See * https://github.com/OpenLightingProject/ola/pull/429 for details. */ void SelectServerTest::testRemoveWriteWhenOtherReadable() { Descriptors read_set, write_set, delete_set; LoopbackDescriptor *loopback = new LoopbackDescriptor(); loopback->Init(); loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler)); write_set.insert(loopback); delete_set.insert(loopback); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback)); OLA_ASSERT_EQ(1, write_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); m_ss->Execute(NewSingleCallback( this, &SelectServerTest::RemoveAndDeleteDescriptors, read_set, write_set, delete_set)); m_ss->Run(); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); }