1 /* 2 * Copyright 2018, 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.common; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import org.junit.Rule; 22 import org.junit.Test; 23 import org.junit.rules.ExpectedException; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 /** Unit tests for {@link ServerStats}. */ 28 @RunWith(JUnit4.class) 29 public class ServerStatsTest { 30 31 @Rule public ExpectedException thrown = ExpectedException.none(); 32 33 @Test serverStatsCreate()34 public void serverStatsCreate() { 35 ServerStats serverStats = null; 36 37 serverStats = ServerStats.create(31, 22, (byte) 0); 38 assertThat(serverStats.getLbLatencyNs()).isEqualTo(31); 39 assertThat(serverStats.getServiceLatencyNs()).isEqualTo(22); 40 assertThat(serverStats.getTraceOption()).isEqualTo((byte) 0); 41 42 serverStats = ServerStats.create(1000011L, 900022L, (byte) 1); 43 assertThat(serverStats.getLbLatencyNs()).isEqualTo(1000011L); 44 assertThat(serverStats.getServiceLatencyNs()).isEqualTo(900022L); 45 assertThat(serverStats.getTraceOption()).isEqualTo((byte) 1); 46 47 serverStats = ServerStats.create(0, 22, (byte) 0); 48 assertThat(serverStats.getLbLatencyNs()).isEqualTo(0); 49 assertThat(serverStats.getServiceLatencyNs()).isEqualTo(22); 50 assertThat(serverStats.getTraceOption()).isEqualTo((byte) 0); 51 52 serverStats = ServerStats.create(1010, 0, (byte) 0); 53 assertThat(serverStats.getLbLatencyNs()).isEqualTo(1010); 54 assertThat(serverStats.getServiceLatencyNs()).isEqualTo(0); 55 assertThat(serverStats.getTraceOption()).isEqualTo((byte) 0); 56 } 57 58 @Test create_LbLatencyNegative()59 public void create_LbLatencyNegative() { 60 thrown.expect(IllegalArgumentException.class); 61 thrown.expectMessage("'getLbLatencyNs' is less than zero"); 62 ServerStats.create(-1L, 100, (byte) 0); 63 } 64 65 @Test create_ServerLatencyNegative()66 public void create_ServerLatencyNegative() { 67 thrown.expect(IllegalArgumentException.class); 68 thrown.expectMessage("'getServiceLatencyNs' is less than zero"); 69 ServerStats.create(100L, -1L, (byte) 0); 70 } 71 72 @Test create_LbLatencyAndServerLatencyNegative()73 public void create_LbLatencyAndServerLatencyNegative() { 74 thrown.expect(IllegalArgumentException.class); 75 thrown.expectMessage("'getLbLatencyNs' is less than zero"); 76 ServerStats.create(-100L, -1L, (byte) 0); 77 } 78 } 79