1// Copyright 2020 Google LLC 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// https://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 14package wire 15 16import ( 17 "testing" 18 "time" 19) 20 21func TestValidatePublishSettings(t *testing.T) { 22 for _, tc := range []struct { 23 desc string 24 // mutateSettings is passed a copy of DefaultPublishSettings to mutate. 25 mutateSettings func(settings *PublishSettings) 26 wantErr bool 27 }{ 28 { 29 desc: "valid: default", 30 mutateSettings: func(settings *PublishSettings) {}, 31 wantErr: false, 32 }, 33 { 34 desc: "valid: max", 35 mutateSettings: func(settings *PublishSettings) { 36 settings.CountThreshold = MaxPublishRequestCount 37 settings.ByteThreshold = MaxPublishRequestBytes 38 // These have no max bounds, check large values. 39 settings.DelayThreshold = 24 * time.Hour 40 settings.Timeout = 24 * time.Hour 41 settings.BufferedByteLimit = 1e10 42 }, 43 wantErr: false, 44 }, 45 { 46 desc: "invalid: zero CountThreshold", 47 mutateSettings: func(settings *PublishSettings) { 48 settings.CountThreshold = 0 49 }, 50 wantErr: true, 51 }, 52 { 53 desc: "invalid: CountThreshold over MaxPublishRequestCount", 54 mutateSettings: func(settings *PublishSettings) { 55 settings.CountThreshold = MaxPublishRequestCount + 1 56 }, 57 wantErr: true, 58 }, 59 { 60 desc: "invalid: ByteThreshold over MaxPublishRequestBytes", 61 mutateSettings: func(settings *PublishSettings) { 62 settings.ByteThreshold = MaxPublishRequestBytes + 1 63 }, 64 wantErr: true, 65 }, 66 { 67 desc: "invalid: zero ByteThreshold", 68 mutateSettings: func(settings *PublishSettings) { 69 settings.ByteThreshold = 0 70 }, 71 wantErr: true, 72 }, 73 { 74 desc: "invalid: zero DelayThreshold", 75 mutateSettings: func(settings *PublishSettings) { 76 settings.DelayThreshold = time.Duration(0) 77 }, 78 wantErr: true, 79 }, 80 { 81 desc: "invalid: zero Timeout", 82 mutateSettings: func(settings *PublishSettings) { 83 settings.Timeout = time.Duration(0) 84 }, 85 wantErr: true, 86 }, 87 { 88 desc: "invalid: zero BufferedByteLimit", 89 mutateSettings: func(settings *PublishSettings) { 90 settings.BufferedByteLimit = 0 91 }, 92 wantErr: true, 93 }, 94 } { 95 t.Run(tc.desc, func(t *testing.T) { 96 settings := DefaultPublishSettings 97 tc.mutateSettings(&settings) 98 99 gotErr := validatePublishSettings(settings) 100 if (gotErr != nil) != tc.wantErr { 101 t.Errorf("validatePublishSettings(%v) = %v, want err=%v", settings, gotErr, tc.wantErr) 102 } 103 }) 104 } 105} 106 107func TestValidateReceiveSettings(t *testing.T) { 108 for _, tc := range []struct { 109 desc string 110 // mutateSettings is passed a copy of DefaultReceiveSettings to mutate. 111 mutateSettings func(settings *ReceiveSettings) 112 wantErr bool 113 }{ 114 { 115 desc: "valid: default", 116 mutateSettings: func(settings *ReceiveSettings) {}, 117 wantErr: false, 118 }, 119 { 120 desc: "valid: max", 121 mutateSettings: func(settings *ReceiveSettings) { 122 settings.Partitions = []int{5, 3, 9, 1, 4, 0} 123 // These have no max bounds, check large values. 124 settings.MaxOutstandingMessages = 100000 125 settings.MaxOutstandingBytes = 1e10 126 settings.Timeout = 24 * time.Hour 127 }, 128 wantErr: false, 129 }, 130 { 131 desc: "invalid: zero MaxOutstandingMessages", 132 mutateSettings: func(settings *ReceiveSettings) { 133 settings.MaxOutstandingMessages = 0 134 }, 135 wantErr: true, 136 }, 137 { 138 desc: "invalid: zero MaxOutstandingBytes", 139 mutateSettings: func(settings *ReceiveSettings) { 140 settings.MaxOutstandingBytes = 0 141 }, 142 wantErr: true, 143 }, 144 { 145 desc: "invalid: negative partition", 146 mutateSettings: func(settings *ReceiveSettings) { 147 settings.Partitions = []int{0, -1} 148 }, 149 wantErr: true, 150 }, 151 { 152 desc: "invalid: duplicate partition", 153 mutateSettings: func(settings *ReceiveSettings) { 154 settings.Partitions = []int{0, 1, 2, 3, 4, 1} 155 }, 156 wantErr: true, 157 }, 158 } { 159 t.Run(tc.desc, func(t *testing.T) { 160 settings := DefaultReceiveSettings 161 tc.mutateSettings(&settings) 162 163 gotErr := validateReceiveSettings(settings) 164 if (gotErr != nil) != tc.wantErr { 165 t.Errorf("validateReceiveSettings(%v) = %v, want err=%v", settings, gotErr, tc.wantErr) 166 } 167 }) 168 } 169} 170