...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package testutil
16
17 import (
18 "fmt"
19
20 "github.com/google/go-cmp/cmp"
21 )
22
23
24 type OrderedKeyMsg struct {
25 Key string
26 Data string
27 }
28
29
30
31
32
33 func VerifyKeyOrdering(publishData, receiveData []OrderedKeyMsg) error {
34 publishKeyMap := make(map[string][]string)
35 receiveKeyMap := make(map[string][]string)
36
37 for _, d := range publishData {
38 if d.Key == "" {
39 continue
40 }
41 publishKeyMap[d.Key] = append(publishKeyMap[d.Key], d.Data)
42 }
43
44 for _, d := range receiveData {
45 if d.Key == "" {
46 continue
47 }
48 receiveKeyMap[d.Key] = append(receiveKeyMap[d.Key], d.Data)
49 }
50
51 if len(publishKeyMap) != len(receiveKeyMap) {
52 return fmt.Errorf("published %d keys, received %d - expected them to be equal but they were not", len(publishKeyMap), len(receiveKeyMap))
53 }
54
55 for k, pb := range publishKeyMap {
56 rd, ok := receiveKeyMap[k]
57 if !ok {
58
59
60
61 return fmt.Errorf("saw key %s, but we never published this key", k)
62 }
63
64 if diff := cmp.Diff(rd, pb); diff != "" {
65 return fmt.Errorf("%s: got -, want +\n\t%s", k, diff)
66 }
67 }
68
69 return nil
70 }
71
View as plain text