File tree Expand file tree Collapse file tree 3 files changed +31
-6
lines changed
main/java/software/amazon/cloudwatchlogs/emf/sinks
test/java/software/amazon/cloudwatchlogs/emf/sinks Expand file tree Collapse file tree 3 files changed +31
-6
lines changed Original file line number Diff line number Diff line change 1
- FROM openjdk:8 -jdk-slim
1
+ FROM openjdk:11 -jdk-slim
2
2
RUN mkdir /app
3
3
COPY build/libs/*.jar /app/agent.jar
4
4
ENV JAVA_OPTS=""
Original file line number Diff line number Diff line change @@ -51,9 +51,7 @@ protected Socket createSocket() {
51
51
52
52
@ Override
53
53
public synchronized void sendMessage (String message ) {
54
- if (socket == null || socket .isClosed () || shouldConnect ) {
55
- connect ();
56
- }
54
+ checkConnection ();
57
55
58
56
OutputStream os ;
59
57
try {
@@ -65,13 +63,22 @@ public synchronized void sendMessage(String message) {
65
63
}
66
64
67
65
try {
66
+ // Write a space to the socket to verify connection before sending event
67
+ os .write (32 );
68
+
68
69
os .write (message .getBytes ());
69
- } catch (Exception e ) {
70
+ } catch (IOException e ) {
70
71
shouldConnect = true ;
71
72
throw new RuntimeException ("Failed to write message to the socket." , e );
72
73
}
73
74
}
74
75
76
+ private void checkConnection () {
77
+ if (socket == null || socket .isClosed () || shouldConnect ) {
78
+ connect ();
79
+ }
80
+ }
81
+
75
82
@ Override
76
83
public void close () throws IOException {
77
84
if (socket != null ) {
Original file line number Diff line number Diff line change 22
22
23
23
import java .io .ByteArrayOutputStream ;
24
24
import java .io .IOException ;
25
+ import java .net .ServerSocket ;
25
26
import java .net .Socket ;
26
27
import org .junit .Test ;
27
28
@@ -45,7 +46,24 @@ protected Socket createSocket() {
45
46
46
47
String message = "Test message" ;
47
48
client .sendMessage (message );
49
+ client .close ();
48
50
49
- assertEquals (bos .toString (), message );
51
+ assertEquals (message , bos .toString ().trim ());
52
+ }
53
+
54
+ @ Test (timeout = 5000 )
55
+ public void testSendMessageWithSocketServer () throws IOException {
56
+ TCPClient client = new TCPClient (new Endpoint ("0.0.0.0" , 9999 , Protocol .TCP ));
57
+ ServerSocket server = new ServerSocket (9999 );
58
+ client .sendMessage ("Test message" );
59
+ Socket socket = server .accept ();
60
+
61
+ byte [] bytes = new byte [1024 ];
62
+ int read = socket .getInputStream ().read (bytes );
63
+ String message = new String (bytes , 0 , read );
64
+ socket .close ();
65
+ server .close ();
66
+
67
+ assertEquals ("Test message" , message .trim ());
50
68
}
51
69
}
You can’t perform that action at this time.
0 commit comments