1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.trace;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import io.opencensus.common.Timestamp;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /** Unit tests for {@link NetworkEvent}. */
27 @RunWith(JUnit4.class)
28 public class NetworkEventTest {
29   @Test(expected = NullPointerException.class)
buildNetworkEvent_NullType()30   public void buildNetworkEvent_NullType() {
31     NetworkEvent.builder(null, 1L).build();
32   }
33 
34   @Test
buildNetworkEvent_WithRequiredFields()35   public void buildNetworkEvent_WithRequiredFields() {
36     NetworkEvent networkEvent = NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).build();
37     assertThat(networkEvent.getType()).isEqualTo(NetworkEvent.Type.SENT);
38     assertThat(networkEvent.getMessageId()).isEqualTo(1L);
39     assertThat(networkEvent.getKernelTimestamp()).isNull();
40     assertThat(networkEvent.getUncompressedMessageSize()).isEqualTo(0L);
41   }
42 
43   @Test
buildNetworkEvent_WithTimestamp()44   public void buildNetworkEvent_WithTimestamp() {
45     NetworkEvent networkEvent =
46         NetworkEvent.builder(NetworkEvent.Type.SENT, 1L)
47             .setKernelTimestamp(Timestamp.fromMillis(123456L))
48             .build();
49     assertThat(networkEvent.getKernelTimestamp()).isEqualTo(Timestamp.fromMillis(123456L));
50     assertThat(networkEvent.getType()).isEqualTo(NetworkEvent.Type.SENT);
51     assertThat(networkEvent.getMessageId()).isEqualTo(1L);
52     assertThat(networkEvent.getUncompressedMessageSize()).isEqualTo(0L);
53   }
54 
55   @Test
buildNetworkEvent_WithUncompressedMessageSize()56   public void buildNetworkEvent_WithUncompressedMessageSize() {
57     NetworkEvent networkEvent =
58         NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).setUncompressedMessageSize(123L).build();
59     assertThat(networkEvent.getKernelTimestamp()).isNull();
60     assertThat(networkEvent.getType()).isEqualTo(NetworkEvent.Type.SENT);
61     assertThat(networkEvent.getMessageId()).isEqualTo(1L);
62     assertThat(networkEvent.getUncompressedMessageSize()).isEqualTo(123L);
63     assertThat(networkEvent.getMessageSize()).isEqualTo(123L);
64   }
65 
66   @Test
buildNetworkEvent_WithMessageSize()67   public void buildNetworkEvent_WithMessageSize() {
68     NetworkEvent networkEvent =
69         NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).setMessageSize(123L).build();
70     assertThat(networkEvent.getKernelTimestamp()).isNull();
71     assertThat(networkEvent.getType()).isEqualTo(NetworkEvent.Type.SENT);
72     assertThat(networkEvent.getMessageId()).isEqualTo(1L);
73     assertThat(networkEvent.getMessageSize()).isEqualTo(123L);
74     assertThat(networkEvent.getUncompressedMessageSize()).isEqualTo(123L);
75   }
76 
77   @Test
buildNetworkEvent_WithCompressedMessageSize()78   public void buildNetworkEvent_WithCompressedMessageSize() {
79     NetworkEvent networkEvent =
80         NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).setCompressedMessageSize(123L).build();
81     assertThat(networkEvent.getKernelTimestamp()).isNull();
82     assertThat(networkEvent.getType()).isEqualTo(NetworkEvent.Type.SENT);
83     assertThat(networkEvent.getMessageId()).isEqualTo(1L);
84     assertThat(networkEvent.getCompressedMessageSize()).isEqualTo(123L);
85   }
86 
87   @Test
buildNetworkEvent_WithAllValues()88   public void buildNetworkEvent_WithAllValues() {
89     NetworkEvent networkEvent =
90         NetworkEvent.builder(NetworkEvent.Type.RECV, 1L)
91             .setKernelTimestamp(Timestamp.fromMillis(123456L))
92             .setUncompressedMessageSize(123L)
93             .setCompressedMessageSize(63L)
94             .build();
95     assertThat(networkEvent.getKernelTimestamp()).isEqualTo(Timestamp.fromMillis(123456L));
96     assertThat(networkEvent.getType()).isEqualTo(NetworkEvent.Type.RECV);
97     assertThat(networkEvent.getMessageId()).isEqualTo(1L);
98     assertThat(networkEvent.getUncompressedMessageSize()).isEqualTo(123L);
99     // Test that getMessageSize returns same as getUncompressedMessageSize();
100     assertThat(networkEvent.getMessageSize()).isEqualTo(123L);
101     assertThat(networkEvent.getCompressedMessageSize()).isEqualTo(63L);
102   }
103 
104   @Test
networkEvent_ToString()105   public void networkEvent_ToString() {
106     NetworkEvent networkEvent =
107         NetworkEvent.builder(NetworkEvent.Type.SENT, 1L)
108             .setKernelTimestamp(Timestamp.fromMillis(123456L))
109             .setUncompressedMessageSize(123L)
110             .setCompressedMessageSize(63L)
111             .build();
112     assertThat(networkEvent.toString()).contains(Timestamp.fromMillis(123456L).toString());
113     assertThat(networkEvent.toString()).contains("type=SENT");
114     assertThat(networkEvent.toString()).contains("messageId=1");
115     assertThat(networkEvent.toString()).contains("compressedMessageSize=63");
116     assertThat(networkEvent.toString()).contains("uncompressedMessageSize=123");
117   }
118 }
119