Browse Source

Tests: Add should_error_when_connection_fails test to TestLibCoreStream

This test makes sure that Socket classes such as TCPSocket properly
return an error when connection fails rather than crashing or creating
an invalid object.
sin-ack 3 years ago
parent
commit
c63feb4f09
1 changed files with 12 additions and 0 deletions
  1. 12 0
      Tests/LibCore/TestLibCoreStream.cpp

+ 12 - 0
Tests/LibCore/TestLibCoreStream.cpp

@@ -140,6 +140,18 @@ TEST_CASE(file_adopt_invalid_fd)
 
 // TCPSocket tests
 
+TEST_CASE(should_error_when_connection_fails)
+{
+    // NOTE: This is required here because Core::TCPSocket requires
+    //       Core::EventLoop through Core::Notifier.
+    Core::EventLoop event_loop;
+
+    auto maybe_tcp_socket = Core::Stream::TCPSocket::connect({ { 127, 0, 0, 1 }, 1234 });
+    EXPECT(maybe_tcp_socket.is_error());
+    EXPECT(maybe_tcp_socket.error().is_syscall());
+    EXPECT(maybe_tcp_socket.error().code() == ECONNREFUSED);
+}
+
 constexpr auto sent_data = "Mr. Watson, come here. I want to see you."sv;
 
 TEST_CASE(tcp_socket_read)