Source file
src/net/dnsclient_test.go
Documentation: net
1
2
3
4
5 package net
6
7 import (
8 "math/rand"
9 "testing"
10 )
11
12 func checkDistribution(t *testing.T, data []*SRV, margin float64) {
13 sum := 0
14 for _, srv := range data {
15 sum += int(srv.Weight)
16 }
17
18 results := make(map[string]int)
19
20 count := 1000
21 for j := 0; j < count; j++ {
22 d := make([]*SRV, len(data))
23 copy(d, data)
24 byPriorityWeight(d).shuffleByWeight()
25 key := d[0].Target
26 results[key] = results[key] + 1
27 }
28
29 actual := results[data[0].Target]
30 expected := float64(count) * float64(data[0].Weight) / float64(sum)
31 diff := float64(actual) - expected
32 t.Logf("actual: %v diff: %v e: %v m: %v", actual, diff, expected, margin)
33 if diff < 0 {
34 diff = -diff
35 }
36 if diff > (expected * margin) {
37 t.Errorf("missed target weight: expected %v, %v", expected, actual)
38 }
39 }
40
41 func testUniformity(t *testing.T, size int, margin float64) {
42 rand.Seed(1)
43 data := make([]*SRV, size)
44 for i := 0; i < size; i++ {
45 data[i] = &SRV{Target: string('a' + rune(i)), Weight: 1}
46 }
47 checkDistribution(t, data, margin)
48 }
49
50 func TestDNSSRVUniformity(t *testing.T) {
51 testUniformity(t, 2, 0.05)
52 testUniformity(t, 3, 0.10)
53 testUniformity(t, 10, 0.20)
54 testWeighting(t, 0.05)
55 }
56
57 func testWeighting(t *testing.T, margin float64) {
58 rand.Seed(1)
59 data := []*SRV{
60 {Target: "a", Weight: 60},
61 {Target: "b", Weight: 30},
62 {Target: "c", Weight: 10},
63 }
64 checkDistribution(t, data, margin)
65 }
66
67 func TestWeighting(t *testing.T) {
68 testWeighting(t, 0.05)
69 }
70
View as plain text