Example #1
0
/*
 * Test UDP sockets work correctly.
 * The client connects and the server sends some data. The client checks the
 * data matches and then closes the connection.
 */
void SocketTest::testUDPSocket() {
  IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0);
  UDPSocket socket;
  OLA_ASSERT_TRUE(socket.Init());
  OLA_ASSERT_FALSE(socket.Init());
  OLA_ASSERT_TRUE(socket.Bind(socket_address));
  OLA_ASSERT_FALSE(socket.Bind(socket_address));

  IPV4SocketAddress local_address;
  OLA_ASSERT_TRUE(socket.GetSocketAddress(&local_address));
  OLA_ASSERT_EQ(static_cast<uint16_t>(AF_INET), local_address.Family());

  socket.SetOnData(
      ola::NewCallback(this, &SocketTest::UDPReceiveAndSend, &socket));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));

  UDPSocket client_socket;
  OLA_ASSERT_TRUE(client_socket.Init());
  OLA_ASSERT_FALSE(client_socket.Init());

  client_socket.SetOnData(
      ola::NewCallback(
        this, &SocketTest::UDPReceiveAndTerminate,
        static_cast<UDPSocket*>(&client_socket)));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&client_socket));

  ssize_t bytes_sent = client_socket.SendTo(
      static_cast<const uint8_t*>(test_cstring),
      sizeof(test_cstring),
      local_address);
  OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
  m_ss->Run();
  m_ss->RemoveReadDescriptor(&socket);
  m_ss->RemoveReadDescriptor(&client_socket);
}
Example #2
0
/**
 * Generic method to test server initiated close
 */
void SocketTest::SocketServerClose(ConnectedDescriptor *socket,
                                   ConnectedDescriptor *socket2) {
  OLA_ASSERT_NOT_NULL(socket);
  socket->SetOnData(ola::NewCallback(
        this, &SocketTest::Receive,
        static_cast<ConnectedDescriptor*>(socket)));
  socket->SetOnClose(
      ola::NewSingleCallback(this, &SocketTest::TerminateOnClose));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(socket));

  OLA_ASSERT_TRUE(socket2);
  socket2->SetOnData(ola::NewCallback(
        this, &SocketTest::ReceiveSendAndClose,
        static_cast<ConnectedDescriptor*>(socket2)));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(socket2));

  ssize_t bytes_sent = socket->Send(
      static_cast<const uint8_t*>(test_cstring),
      sizeof(test_cstring));
  OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
  m_ss->Run();
  m_ss->RemoveReadDescriptor(socket);
  m_ss->RemoveReadDescriptor(socket2);
  delete socket2;
}
Example #3
0
/*
 * Test UDP sockets with an IOQueue work correctly.
 * The client connects and the server sends some data. The client checks the
 * data matches and then closes the connection.
 */
void SocketTest::testIOQueueUDPSend() {
  IPV4SocketAddress socket_address(IPV4Address::Loopback(), 9010);
  UDPSocket socket;
  OLA_ASSERT_TRUE(socket.Init());
  OLA_ASSERT_FALSE(socket.Init());
  OLA_ASSERT_TRUE(socket.Bind(socket_address));
  OLA_ASSERT_FALSE(socket.Bind(socket_address));

  socket.SetOnData(
      ola::NewCallback(this, &SocketTest::UDPReceiveAndSend, &socket));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));

  UDPSocket client_socket;
  OLA_ASSERT_TRUE(client_socket.Init());
  OLA_ASSERT_FALSE(client_socket.Init());

  client_socket.SetOnData(
      ola::NewCallback(
        this, &SocketTest::UDPReceiveAndTerminate,
        static_cast<UDPSocket*>(&client_socket)));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&client_socket));

  IOQueue output;
  output.Write(static_cast<const uint8_t*>(test_cstring), sizeof(test_cstring));
  ssize_t bytes_sent = client_socket.SendTo(&output, socket_address);
  OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
  m_ss->Run();
  m_ss->RemoveReadDescriptor(&socket);
  m_ss->RemoveReadDescriptor(&client_socket);
}
Example #4
0
/*
 * Check that we don't invalid iterators by removing descriptors during an
 * on_read callback.
 */
void SelectServerTest::testRemoveOthersWhenReadable() {
  Descriptors read_set, write_set, delete_set;
  LoopbackDescriptor loopback1, loopback2, loopback3;
  loopback1.Init();
  loopback2.Init();
  loopback3.Init();

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback1));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback2));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback3));

  read_set.insert(&loopback1);
  read_set.insert(&loopback2);
  read_set.insert(&loopback3);

  loopback2.SetOnClose(NewSingleCallback(
      this,
      &SelectServerTest::RemoveAndDeleteDescriptors,
      read_set, write_set, delete_set));

  OLA_ASSERT_EQ(0, write_descriptor_count->Get());
  OLA_ASSERT_EQ(4, connected_read_descriptor_count->Get());

  loopback2.CloseClient();
  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());
}
Example #5
0
/*
 * Test TCP sockets work correctly.
 * The client connects and the server then sends some data and closes the
 * connection.
 */
void SocketTest::testTCPSocketServerClose() {
  IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0);
  ola::network::TCPSocketFactory socket_factory(
      ola::NewCallback(this, &SocketTest::NewConnectionSendAndClose));
  TCPAcceptingSocket socket(&socket_factory);
  OLA_ASSERT_TRUE_MSG(socket.Listen(socket_address),
                      "Check for another instance of olad running");

  OLA_ASSERT_FALSE(socket.Listen(socket_address));

  GenericSocketAddress local_address = socket.GetLocalAddress();
  OLA_ASSERT_EQ(static_cast<uint16_t>(AF_INET), local_address.Family());

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));

  // The client socket checks the response and terminates on close
  TCPSocket *client_socket = TCPSocket::Connect(local_address);
  OLA_ASSERT_NOT_NULL(client_socket);

  client_socket->SetOnData(ola::NewCallback(
        this, &SocketTest::Receive,
        static_cast<ConnectedDescriptor*>(client_socket)));
  client_socket->SetOnClose(
      ola::NewSingleCallback(this, &SocketTest::TerminateOnClose));
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(client_socket));

  m_ss->Run();
  m_ss->RemoveReadDescriptor(&socket);
  m_ss->RemoveReadDescriptor(client_socket);
  delete client_socket;
}
Example #6
0
/*
 * Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
 * export map is updated.
 */
void SelectServerTest::testAddRemoveReadDescriptor() {
  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());

  LoopbackDescriptor loopback;
  loopback.Init();

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback));
  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());

  // Add a udp socket
  UDPSocket udp_socket;
  OLA_ASSERT_TRUE(udp_socket.Init());
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&udp_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Check remove works
  m_ss->RemoveReadDescriptor(&loopback);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  m_ss->RemoveReadDescriptor(&udp_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());
}
Example #7
0
/*
 * 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);
}
Example #8
0
/*
 * Test non-blocking TCP connects work correctly.
 */
void TCPConnectorTest::testNonBlockingConnect() {
  ola::network::TCPSocketFactory socket_factory(
      ola::NewCallback(this, &TCPConnectorTest::AcceptedConnection));


  TCPAcceptingSocket listening_socket(&socket_factory);
  IPV4SocketAddress listen_address(m_localhost, 0);
  OLA_ASSERT_TRUE_MSG(listening_socket.Listen(listen_address),
                      "Failed to listen");
  GenericSocketAddress addr = listening_socket.GetLocalAddress();
  OLA_ASSERT_TRUE(addr.IsValid());

  // calling listen a second time should fail
  OLA_ASSERT_FALSE(listening_socket.Listen(listen_address));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&listening_socket));

  // now attempt a non-blocking connect
  // because we're connecting to the localhost this may run this callback
  // immediately.
  TCPConnector connector(m_ss);
  TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
  TCPConnector::TCPConnectionID id = connector.Connect(
      addr.V4Addr(),
      connect_timeout,
      ola::NewSingleCallback(this, &TCPConnectorTest::OnConnect));
  OLA_ASSERT_TRUE(id);

  m_ss->Run();
  OLA_ASSERT_EQ(0u, connector.ConnectionsPending());
  m_ss->RemoveReadDescriptor(&listening_socket);
}
Example #9
0
/*
 * 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());
}
Example #10
0
/*
 * 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));
}
/**
 * Check pausing doesn't mark the channel as bad.
 */
void HealthCheckedConnectionTest::testPauseAndResume() {
  MockHealthCheckedConnection connection(&socket,
                                         &m_ss,
                                         heartbeat_interval,
                                         options,
                                         &m_clock);
  socket.SetOnData(
      NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
  connection.Setup();
  m_ss.AddReadDescriptor(&socket);
  connection.Setup();

  m_ss.RegisterSingleTimeout(
      TimeInterval(1, 0),
      NewSingleCallback(this,
                        &HealthCheckedConnectionTest::PauseReading,
                        &connection));
  m_ss.RegisterSingleTimeout(
      TimeInterval(3, 0),
      NewSingleCallback(this,
                        &HealthCheckedConnectionTest::ResumeReading,
                        &connection));

  m_ss.Run();
  OLA_ASSERT_TRUE(connection.ChannelOk());
}
Example #12
0
 void OnTCPConnect(TCPSocket *socket) {
   OLA_INFO << "New connection: " << socket;
   socket->SetOnData(
       NewCallback(this, &Server::ReceiveTCPData, socket));
   socket->SetOnClose(
       NewSingleCallback(this, &Server::SocketClosed, socket));
   m_ss.AddReadDescriptor(socket);
   m_sockets.push_back(socket);
 }
Example #13
0
void RpcChannelTest::setUp() {
  m_socket = new LoopbackDescriptor();
  m_socket->Init();

  m_service = new TestServiceImpl(&m_ss);
  m_channel = new RpcChannel(m_service, m_socket);
  m_ss.AddReadDescriptor(m_socket);
  m_stub = new TestService_Stub(m_channel);
}
/**
 * Setup a TCP socket that accepts connections
 */
void AdvancedTCPConnectorTest::SetupListeningSocket(
    TcpAcceptingSocket *listening_socket) {
  CPPUNIT_ASSERT_MESSAGE(
      "Check for another instance of olad running",
      listening_socket->Listen(m_localhost, SERVER_PORT));
  // calling listen a second time should fail
  CPPUNIT_ASSERT(!listening_socket->Listen(m_localhost, SERVER_PORT));
  OLA_INFO << "listening on " <<SERVER_PORT;
  CPPUNIT_ASSERT(m_ss->AddReadDescriptor(listening_socket));
}
Example #15
0
/*
 * Accept a new TCP connection.
 */
void AdvancedTCPConnectorTest::AcceptedConnection(TCPSocket *new_socket) {
  OLA_ASSERT_NOT_NULL(new_socket);
  GenericSocketAddress address = new_socket->GetPeerAddress();
  OLA_ASSERT_TRUE(address.Family() == AF_INET);
  OLA_INFO << "Connection from " << address;

  // terminate the ss when this connection is closed
  new_socket->SetOnClose(
    ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::TerminateOnClose));
  m_ss->AddReadDescriptor(new_socket, true);
}
Example #16
0
/*
 * Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
 * export map is updated.
 */
void SelectServerTest::testAddRemoveReadDescriptor() {
  LoopbackDescriptor bad_socket;
  IntegerVariable *connected_socket_count =
    m_map->GetIntegerVar(PollerInterface::K_CONNECTED_DESCRIPTORS_VAR);
  IntegerVariable *socket_count =
    m_map->GetIntegerVar(PollerInterface::K_READ_DESCRIPTOR_VAR);
  OLA_ASSERT_EQ(1, connected_socket_count->Get());  // internal socket
  OLA_ASSERT_EQ(0, socket_count->Get());
  // adding and removin a non-connected socket should fail
  OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&bad_socket));
  OLA_ASSERT_FALSE(m_ss->RemoveReadDescriptor(&bad_socket));

  LoopbackDescriptor loopback_socket;
  loopback_socket.Init();
  OLA_ASSERT_EQ(1, connected_socket_count->Get());
  OLA_ASSERT_EQ(0, socket_count->Get());
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
  // Adding a second time should fail
  OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&loopback_socket));
  OLA_ASSERT_EQ(2, connected_socket_count->Get());
  OLA_ASSERT_EQ(0, socket_count->Get());

  // Add a udp socket
  UDPSocket udp_socket;
  OLA_ASSERT_TRUE(udp_socket.Init());
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&udp_socket));
  OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&udp_socket));
  OLA_ASSERT_EQ(2, connected_socket_count->Get());
  OLA_ASSERT_EQ(1, socket_count->Get());

  // Check remove works
  OLA_ASSERT_TRUE(m_ss->RemoveReadDescriptor(&loopback_socket));
  OLA_ASSERT_EQ(1, connected_socket_count->Get());
  OLA_ASSERT_EQ(1, socket_count->Get());
  OLA_ASSERT_TRUE(m_ss->RemoveReadDescriptor(&udp_socket));
  OLA_ASSERT_EQ(1, connected_socket_count->Get());
  OLA_ASSERT_EQ(0, socket_count->Get());

  // Remove again should fail
  OLA_ASSERT_FALSE(m_ss->RemoveReadDescriptor(&loopback_socket));
}
/*
 * Accept a new TCP connection.
 */
void AdvancedTCPConnectorTest::AcceptedConnection(TcpSocket *new_socket) {
  CPPUNIT_ASSERT(new_socket);
  IPV4Address address;
  uint16_t port;
  CPPUNIT_ASSERT(new_socket->GetPeer(&address, &port));
  OLA_INFO << "Connection from " << address << ":" << port;

  // terminate the ss when this connection is closed
  new_socket->SetOnClose(
    ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::TerminateOnClose));
  m_ss->AddReadDescriptor(new_socket, true);
}
/*
 * Flashes the device
 */
int main(int argc, char *argv[]) {
  options opts;
  opts.log_level = ola::OLA_LOG_WARN;
  opts.help = false;
  opts.firmware = DEFAULT_FIRMWARE;
  opts.device = DEFAULT_DEVICE;
  ParseOptions(argc, argv, &opts);

  if (opts.help)
    DisplayHelpAndExit(argv);
  ola::InitLogging(opts.log_level, ola::OLA_LOG_STDERR);

  ifstream firmware_file(opts.firmware.data());

  if (!firmware_file.is_open()) {
    OLA_FATAL << "Can't open the firmware file " << opts.firmware << ": " <<
      strerror(errno);
    exit(1);
  }

  SelectServer ss;

  ola::io::ConnectedDescriptor *descriptor =
     ola::plugin::usbpro::BaseUsbProWidget::OpenDevice(opts.device);
  if (!descriptor)
    exit(ola::EXIT_UNAVAILABLE);

  descriptor->SetOnClose(ola::NewSingleCallback(&Stop, &ss));
  ss.AddReadDescriptor(descriptor);
  DispatchingUsbProWidget widget(descriptor, NULL);
  FirmwareTransferer transferer(&firmware_file, &widget, &ss);
  widget.SetHandler(
      ola::NewCallback(&transferer, &FirmwareTransferer::HandleMessage));

  if (!transferer.SendReprogram()) {
    OLA_FATAL << "Send message failed";
    exit(1);
  }

  ss.RegisterSingleTimeout(
      PAUSE_DELAY,
      ola::NewSingleCallback(&transferer, &FirmwareTransferer::StartTransfer));
  widget.GetDescriptor()->SetOnClose(
      ola::NewSingleCallback(&transferer, &FirmwareTransferer::AbortTransfer));
  ss.RegisterSingleTimeout(
      ABORT_TIMEOUT,
      ola::NewSingleCallback(&transferer, &FirmwareTransferer::AbortTransfer));
  ss.Run();

  firmware_file.close();
  return !transferer.WasSucessfull();
}
Example #19
0
/*
 * Accept a new connection and send some test data
 */
void SocketTest::NewConnectionSend(TCPSocket *new_socket) {
  OLA_ASSERT_TRUE(new_socket);
  GenericSocketAddress address = new_socket->GetPeerAddress();
  OLA_ASSERT_TRUE(address.Family() == AF_INET);
  OLA_INFO << "Connection from " << address;
  ssize_t bytes_sent = new_socket->Send(
      static_cast<const uint8_t*>(test_cstring),
      sizeof(test_cstring));
  OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
  new_socket->SetOnClose(ola::NewSingleCallback(this,
                                               &SocketTest::TerminateOnClose));
  m_ss->AddReadDescriptor(new_socket, true);
}
Example #20
0
/**
 * Setup a TCP socket that accepts connections
 */
void AdvancedTCPConnectorTest::SetupListeningSocket(
    TCPAcceptingSocket *listening_socket) {
  IPV4SocketAddress listen_address(m_localhost, 0);
  OLA_ASSERT_TRUE_MSG(listening_socket->Listen(listen_address),
                      "Failed to listen");
  // calling listen a second time should fail
  OLA_ASSERT_FALSE(listening_socket->Listen(listen_address));

  GenericSocketAddress addr = listening_socket->GetLocalAddress();
  OLA_ASSERT_TRUE(addr.IsValid());
  m_server_address = addr.V4Addr();
  OLA_INFO << "listening on " << m_server_address;
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(listening_socket));
}
Example #21
0
/*
 * Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
 * export map is updated.
 */
void SelectServerTest::testAddRemoveReadDescriptor() {
  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());

  LoopbackDescriptor bad_socket;
  // adding and removing a non-connected socket should fail
  OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&bad_socket));
  m_ss->RemoveReadDescriptor(&bad_socket);

  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());

  // Add a udp socket
  UDPSocket udp_socket;
  OLA_ASSERT_TRUE(udp_socket.Init());
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&udp_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Check remove works
  m_ss->RemoveReadDescriptor(&loopback_socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  m_ss->RemoveReadDescriptor(&udp_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());
}
Example #22
0
/*
 * 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());
}
/**
 * Check the channel works when every 2nd heartbeat is lost
 */
void HealthCheckedConnectionTest::testChannelWithPacketLoss() {
  options.send_every = 2;
  MockHealthCheckedConnection connection(&socket,
                                         &m_ss,
                                         heartbeat_interval,
                                         options,
                                         &m_clock);

  socket.SetOnData(
      NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
  connection.Setup();
  m_ss.AddReadDescriptor(&socket);
  connection.Setup();

  m_ss.Run();
  OLA_ASSERT_TRUE(connection.ChannelOk());
}
/*
 * Check that the channel stays up when all heartbeats are received.
 */
void HealthCheckedConnectionTest::testSimpleChannel() {
  options.validate_heartbeat = true;
  MockHealthCheckedConnection connection(&socket,
                                         &m_ss,
                                         heartbeat_interval,
                                         options,
                                         &m_clock);

  socket.SetOnData(
      NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
  connection.Setup();
  m_ss.AddReadDescriptor(&socket);
  connection.Setup();

  m_ss.Run();
  OLA_ASSERT_TRUE(connection.ChannelOk());
}
Example #25
0
/*
 * Confirm we correctly detect the remote end closing the connection.
 * This uses the delete_on_close option.
 */
void SelectServerTest::testRemoteEndCloseWithDelete() {
  // Ownership is transferred to the SelectServer.
  LoopbackDescriptor *loopback_socket = new LoopbackDescriptor();
  loopback_socket->Init();
  loopback_socket->SetOnClose(NewSingleCallback(
      this, &SelectServerTest::Terminate));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback_socket, true));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // Now the Write end closes
  loopback_socket->CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Example #26
0
/*
 * Confirm we correctly detect the remote end closing the connection.
 */
void SelectServerTest::testRemoteEndClose() {
  LoopbackDescriptor loopback_socket;
  loopback_socket.Init();
  loopback_socket.SetOnClose(NewSingleCallback(
      this, &SelectServerTest::RemoveFromSelectServer,
      reinterpret_cast<ConnectedDescriptor*>(&loopback_socket)));

  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());

  // now the Write end closes
  loopback_socket.CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Example #27
0
/*
 * 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());
}
Example #28
0
/*
 * Check the delete_on_close feature handles the case where the descriptor
 * being closed is removed from the on_close handler.
 */
void SelectServerTest::testRemoteEndCloseWithRemoveAndDelete() {
  Descriptors read_set, write_set, delete_set;
  LoopbackDescriptor *loopback = new LoopbackDescriptor();
  loopback->Init();

  read_set.insert(loopback);
  loopback->SetOnClose(NewSingleCallback(
      this, &SelectServerTest::RemoveAndDeleteDescriptors,
      read_set, write_set, delete_set));

  // Ownership is transferred.
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback, true));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // Close the write end of the descriptor.
  loopback->CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Example #29
0
/*
 * Confirm we correctly detect the remote end closing the connection.
 */
void SelectServerTest::testRemoteEndClose() {
  Descriptors read_set, write_set, delete_set;
  LoopbackDescriptor loopback;
  loopback.Init();

  read_set.insert(&loopback);

  loopback.SetOnClose(NewSingleCallback(
      this, &SelectServerTest::RemoveAndDeleteDescriptors,
      read_set, write_set, delete_set));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // now the Write end closes
  loopback.CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}