Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func TestDiscoveryManagerThrottlesTheSyncCalls(t *testing.T) {
- // There is no guarantee of the order that the discovery adapters will send their updates
- // so in the expected slice need to cover all combinations including the expected final merged target sets.
- testCases := []struct {
- title string
- updates map[string][]update
- expectedSyncCalls [][]string
- }{
- {
- title: "Single TP empty initials",
- updates: map[string][]update{
- "tp1": {
- {
- targetGroups: []config.TargetGroup{},
- interval: 5,
- },
- },
- },
- expectedSyncCalls: [][]string{
- {},
- },
- },
- {
- title: "Single TP initials only",
- updates: map[string][]update{
- "tp1": {
- {
- targetGroups: []config.TargetGroup{{Source: "initial1"}, {Source: "initial2"}},
- interval: 0,
- },
- },
- },
- expectedSyncCalls: [][]string{
- {"initial1", "initial2"},
- },
- },
- {
- title: "Multiple TPs initials only",
- updates: map[string][]update{
- "tp1": {
- {
- targetGroups: []config.TargetGroup{{Source: "tp1-initial1"}, {Source: "tp1-initial2"}},
- interval: 0,
- },
- },
- "tp2": {
- {
- targetGroups: []config.TargetGroup{{Source: "tp2-initial1"}},
- interval: 0,
- },
- },
- },
- expectedSyncCalls: [][]string{
- // {"tp2-initial1"},
- {"tp1-initial1", "tp1-initial2"},
- {"tp2-initial1", "tp1-initial1", "tp1-initial2"},
- {"tp1-initial1", "tp1-initial2", "tp2-initial1"},
- },
- },
- {
- title: "Multiple TPs with some delayed initials",
- updates: map[string][]update{
- "tp1": {
- {
- targetGroups: []config.TargetGroup{{Source: "tp1-initial1"}, {Source: "tp1-initial2"}},
- interval: 100,
- },
- },
- "tp2": {
- {
- targetGroups: []config.TargetGroup{{Source: "tp2-initial1"}, {Source: "tp2-initial2"}, {Source: "tp2-initial3"}},
- interval: 6000,
- },
- },
- },
- expectedSyncCalls: [][]string{
- {"tp1-initial1", "tp1-initial2"},
- {"tp2-initial1", "tp2-initial2", "tp2-initial3"},
- {"tp1-initial1", "tp1-initial2", "tp2-initial1", "tp2-initial2", "tp2-initial3"},
- {"tp2-initial1", "tp2-initial2", "tp2-initial3", "tp1-initial1", "tp1-initial2"},
- },
- },
- {
- title: "Single TP initials followed by empty updates",
- updates: map[string][]update{
- "tp1": {
- {
- targetGroups: []config.TargetGroup{{Source: "initial1"}, {Source: "initial2"}},
- interval: 0,
- },
- {
- targetGroups: []config.TargetGroup{},
- interval: 10,
- },
- },
- },
- expectedSyncCalls: [][]string{
- {},
- {"initial1", "initial2"},
- },
- },
- }
- for i, testCase := range testCases {
- _ = i
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- discoveryManager := NewManager(ctx, nil)
- defer discoveryManager.cancelDiscoverers()
- go discoveryManager.Run()
- for tpName, update := range testCase.updates {
- tp := newMockDiscoveryProvider(update)
- discoveryManager.startProvider(strconv.Itoa(i), tpName, tp)
- for x := 0; x < len(update); x++ {
- select {
- case <-time.After(20 * time.Second):
- t.Errorf("%d. %q: Test timed out after 20 seconds. This means that the discovery adapter didn't send an expected number of updates", i, testCase.title)
- case tsetMap := <-discoveryManager.SyncCh():
- for _, received := range tsetMap {
- match := false
- for _, expected := range testCase.expectedSyncCalls {
- if len(received) == len(expected) &&
- fmt.Sprintf("%s", received) == fmt.Sprintf("%s", expected) {
- match = true
- }
- }
- if !match {
- t.Errorf("%d. %q: \nreceived : \n%v \nexpected one of: \n%v: ", i, testCase.title, received, testCase.expectedSyncCalls)
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement