|
1 | 1 | package discv5 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math" |
4 | 7 | "testing" |
5 | 8 |
|
6 | 9 | ma "github.com/multiformats/go-multiaddr" |
7 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
8 | 12 |
|
9 | 13 | "github.com/dennis-tra/nebula-crawler/nebtest" |
10 | 14 | ) |
@@ -98,3 +102,90 @@ func Test_sanitizeAddrs(t *testing.T) { |
98 | 102 | }) |
99 | 103 | } |
100 | 104 | } |
| 105 | + |
| 106 | +func TestNoSuccessfulRequest(t *testing.T) { |
| 107 | + tests := []struct { |
| 108 | + name string |
| 109 | + err error |
| 110 | + errorBits uint32 |
| 111 | + want bool |
| 112 | + }{ |
| 113 | + { |
| 114 | + name: "no err", |
| 115 | + err: nil, |
| 116 | + errorBits: 0b00000000, |
| 117 | + want: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "first failed", |
| 121 | + err: fmt.Errorf("some err"), |
| 122 | + errorBits: 0b00000001, |
| 123 | + want: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "second failed, first worked", |
| 127 | + err: fmt.Errorf("some err"), |
| 128 | + errorBits: 0b00000010, |
| 129 | + want: false, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "all four failed", |
| 133 | + err: fmt.Errorf("some err"), |
| 134 | + errorBits: 0b00001111, |
| 135 | + want: true, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "seven failed, one worked", |
| 139 | + err: fmt.Errorf("some err"), |
| 140 | + errorBits: 0b11110111, |
| 141 | + want: false, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "eight failed, but the last one overflowing succeeded (no error)", |
| 145 | + err: nil, |
| 146 | + errorBits: 0b11111111, |
| 147 | + want: false, |
| 148 | + }, |
| 149 | + } |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + if got := noSuccessfulRequest(tt.err, tt.errorBits); got != tt.want { |
| 153 | + t.Errorf("noSuccessfulRequest() = %v, want %v, %s", got, tt.want, tt.name) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + // fail if err is nil |
| 159 | + require.False(t, noSuccessfulRequest(nil, 0)) |
| 160 | + require.False(t, noSuccessfulRequest(nil, 0b11111111)) |
| 161 | + |
| 162 | + err := errors.New("error") |
| 163 | + |
| 164 | + // list of numbers that are power of two minus one |
| 165 | + // for which noSuccessfulRequest should return true |
| 166 | + // because all bits are set (all failures = no success) |
| 167 | + powerOfTwoMinusOneList := []uint32{ |
| 168 | + 0b00000000, |
| 169 | + 0b00000001, |
| 170 | + 0b00000011, |
| 171 | + 0b00000111, |
| 172 | + 0b00001111, |
| 173 | + 0b00011111, |
| 174 | + 0b00111111, |
| 175 | + 0b01111111, |
| 176 | + 0b11111111, |
| 177 | + } |
| 178 | + |
| 179 | + for i := uint32(0); i < uint32(math.Pow(2, 8)); i++ { |
| 180 | + powerOfTwoMinusOne := false |
| 181 | + for _, v := range powerOfTwoMinusOneList { |
| 182 | + if i == v { |
| 183 | + powerOfTwoMinusOne = true |
| 184 | + break |
| 185 | + } |
| 186 | + } |
| 187 | + // assert that noSuccessfulRequest returns true if and only if |
| 188 | + // all bits are set |
| 189 | + require.Equal(t, powerOfTwoMinusOne, noSuccessfulRequest(err, i)) |
| 190 | + } |
| 191 | +} |
0 commit comments