|
| 1 | +// Copyright 2021 ETH Zurich |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestParseParameters(t *testing.T) { |
| 26 | + defaultDuration := time.Second * DefaultDuration // XXX should be defined as Duration |
| 27 | + |
| 28 | + cases := []struct { |
| 29 | + name string |
| 30 | + input string |
| 31 | + inferedPktSize int // input, value inferred from path MTU |
| 32 | + expectedDuration time.Duration |
| 33 | + expectedPacketSize int |
| 34 | + expectedNumPackets int |
| 35 | + expectErr bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "bw 1Mbps", |
| 39 | + input: "1Mbps", |
| 40 | + inferedPktSize: 1400, |
| 41 | + expectedDuration: defaultDuration, |
| 42 | + expectedPacketSize: 1400, |
| 43 | + expectedNumPackets: 267, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "bw 10Mbps", |
| 47 | + input: "10Mbps", |
| 48 | + inferedPktSize: 1400, |
| 49 | + expectedDuration: defaultDuration, |
| 50 | + expectedPacketSize: 1400, |
| 51 | + expectedNumPackets: 2678, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "1s, 1Mbps", |
| 55 | + input: "1,?,?,1Mbps", |
| 56 | + inferedPktSize: 1400, |
| 57 | + expectedDuration: time.Second, |
| 58 | + expectedPacketSize: 1400, |
| 59 | + expectedNumPackets: 89, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "1s, 1Mbps", |
| 63 | + input: "1,?,?,1Mbps", |
| 64 | + inferedPktSize: 1400, |
| 65 | + expectedDuration: time.Second, |
| 66 | + expectedPacketSize: 1400, |
| 67 | + expectedNumPackets: 89, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "computed bw", |
| 71 | + input: "1,1000,1000,?", |
| 72 | + expectedDuration: time.Second, |
| 73 | + expectedPacketSize: 1000, |
| 74 | + expectedNumPackets: 1000, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "computed bw, default pkt size", |
| 78 | + input: "1,?,1000,?", |
| 79 | + inferedPktSize: 1000, |
| 80 | + expectedDuration: time.Second, |
| 81 | + expectedPacketSize: 1000, |
| 82 | + expectedNumPackets: 1000, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "redundant bw", |
| 86 | + input: "1,1000,1000,8Mbps", |
| 87 | + inferedPktSize: 1000, |
| 88 | + expectedDuration: time.Second, |
| 89 | + expectedPacketSize: 1000, |
| 90 | + expectedNumPackets: 1000, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "redundant bw, longer duration", |
| 94 | + input: "10,125,10,1000bps", |
| 95 | + inferedPktSize: 125, |
| 96 | + expectedDuration: 10 * time.Second, |
| 97 | + expectedPacketSize: 125, |
| 98 | + expectedNumPackets: 10, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "redundant bw, plus one packet per second", |
| 102 | + input: "10,125,10,2000bps", // should accept +1 packet/s, so +8*125B == +1000b |
| 103 | + inferedPktSize: 125, |
| 104 | + expectedDuration: 10 * time.Second, |
| 105 | + expectedPacketSize: 125, |
| 106 | + expectedNumPackets: 10, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "redundant bw, minus one packet per second", |
| 110 | + input: "10,125,10,1bps", // should accept -1 packet/s, so -8*125B == -1000b |
| 111 | + inferedPktSize: 125, |
| 112 | + expectedDuration: 10 * time.Second, |
| 113 | + expectedPacketSize: 125, |
| 114 | + expectedNumPackets: 10, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "conflicting bw", |
| 118 | + input: "1,1000,1000,1Mbps", // actual: 8Mbps |
| 119 | + expectErr: true, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "computed packet size", |
| 123 | + input: "3,?,3000,8Mbps", |
| 124 | + inferedPktSize: 100, |
| 125 | + expectedDuration: 3 * time.Second, |
| 126 | + expectedPacketSize: 1000, |
| 127 | + expectedNumPackets: 3000, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, c := range cases { |
| 132 | + t.Run(c.name, func(t *testing.T) { |
| 133 | + ret, err := parseBwtestParameters(c.input, int64(c.inferedPktSize)) |
| 134 | + if c.expectErr { |
| 135 | + assert.Error(t, err) |
| 136 | + } else { |
| 137 | + require.NoError(t, err) |
| 138 | + assert.Equal(t, c.expectedDuration, ret.BwtestDuration, "duration") |
| 139 | + assert.Equal(t, int64(c.expectedPacketSize), ret.PacketSize, "packet size") |
| 140 | + assert.Equal(t, int64(c.expectedNumPackets), ret.NumPackets, "num packets") |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments