Advertisement
kgeorgie

Untitled

Nov 27th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.86 KB | None | 0 0
  1. func TestDiscoveryManagerThrottlesTheSyncCalls(t *testing.T) {
  2.  
  3.     // There is no guarantee of the order that the discovery adapters will send their updates
  4.     // so in the expected slice need to cover all combinations including the expected final merged target sets.
  5.     testCases := []struct {
  6.         title             string
  7.         updates           map[string][]update
  8.         expectedSyncCalls [][]string
  9.     }{
  10.         {
  11.             title: "Single TP empty initials",
  12.             updates: map[string][]update{
  13.                 "tp1": {
  14.                     {
  15.                         targetGroups: []config.TargetGroup{},
  16.                         interval:     5,
  17.                     },
  18.                 },
  19.             },
  20.             expectedSyncCalls: [][]string{
  21.                 {},
  22.             },
  23.         },
  24.  
  25.         {
  26.             title: "Single TP initials only",
  27.             updates: map[string][]update{
  28.                 "tp1": {
  29.                     {
  30.                         targetGroups: []config.TargetGroup{{Source: "initial1"}, {Source: "initial2"}},
  31.                         interval:     0,
  32.                     },
  33.                 },
  34.             },
  35.             expectedSyncCalls: [][]string{
  36.                 {"initial1", "initial2"},
  37.             },
  38.         },
  39.         {
  40.             title: "Multiple TPs initials only",
  41.             updates: map[string][]update{
  42.                 "tp1": {
  43.                     {
  44.                         targetGroups: []config.TargetGroup{{Source: "tp1-initial1"}, {Source: "tp1-initial2"}},
  45.                         interval:     0,
  46.                     },
  47.                 },
  48.                 "tp2": {
  49.                     {
  50.                         targetGroups: []config.TargetGroup{{Source: "tp2-initial1"}},
  51.                         interval:     0,
  52.                     },
  53.                 },
  54.             },
  55.             expectedSyncCalls: [][]string{
  56.                 // {"tp2-initial1"},
  57.                 {"tp1-initial1", "tp1-initial2"},
  58.                 {"tp2-initial1", "tp1-initial1", "tp1-initial2"},
  59.                 {"tp1-initial1", "tp1-initial2", "tp2-initial1"},
  60.             },
  61.         },
  62.         {
  63.             title: "Multiple TPs with some delayed initials",
  64.             updates: map[string][]update{
  65.                 "tp1": {
  66.                     {
  67.                         targetGroups: []config.TargetGroup{{Source: "tp1-initial1"}, {Source: "tp1-initial2"}},
  68.                         interval:     100,
  69.                     },
  70.                 },
  71.                 "tp2": {
  72.                     {
  73.                         targetGroups: []config.TargetGroup{{Source: "tp2-initial1"}, {Source: "tp2-initial2"}, {Source: "tp2-initial3"}},
  74.                         interval:     6000,
  75.                     },
  76.                 },
  77.             },
  78.             expectedSyncCalls: [][]string{
  79.                 {"tp1-initial1", "tp1-initial2"},
  80.                 {"tp2-initial1", "tp2-initial2", "tp2-initial3"},
  81.                 {"tp1-initial1", "tp1-initial2", "tp2-initial1", "tp2-initial2", "tp2-initial3"},
  82.                 {"tp2-initial1", "tp2-initial2", "tp2-initial3", "tp1-initial1", "tp1-initial2"},
  83.             },
  84.         },
  85.         {
  86.             title: "Single TP initials followed by empty updates",
  87.             updates: map[string][]update{
  88.                 "tp1": {
  89.                     {
  90.                         targetGroups: []config.TargetGroup{{Source: "initial1"}, {Source: "initial2"}},
  91.                         interval:     0,
  92.                     },
  93.                     {
  94.                         targetGroups: []config.TargetGroup{},
  95.                         interval:     10,
  96.                     },
  97.                 },
  98.             },
  99.             expectedSyncCalls: [][]string{
  100.                 {},
  101.                 {"initial1", "initial2"},
  102.             },
  103.         },
  104.     }
  105.  
  106.     for i, testCase := range testCases {
  107.         _ = i
  108.         ctx, cancel := context.WithCancel(context.Background())
  109.         defer cancel()
  110.  
  111.         discoveryManager := NewManager(ctx, nil)
  112.         defer discoveryManager.cancelDiscoverers()
  113.  
  114.         go discoveryManager.Run()
  115.  
  116.         for tpName, update := range testCase.updates {
  117.             tp := newMockDiscoveryProvider(update)
  118.             discoveryManager.startProvider(strconv.Itoa(i), tpName, tp)
  119.  
  120.             for x := 0; x < len(update); x++ {
  121.                 select {
  122.                 case <-time.After(20 * time.Second):
  123.                     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)
  124.  
  125.                 case tsetMap := <-discoveryManager.SyncCh():
  126.  
  127.                     for _, received := range tsetMap {
  128.  
  129.                         match := false
  130.                         for _, expected := range testCase.expectedSyncCalls {
  131.                             if len(received) == len(expected) &&
  132.                                 fmt.Sprintf("%s", received) == fmt.Sprintf("%s", expected) {
  133.                                 match = true
  134.                             }
  135.                         }
  136.                         if !match {
  137.                             t.Errorf("%d. %q: \nreceived :  \n%v  \nexpected one of: \n%v: ", i, testCase.title, received, testCase.expectedSyncCalls)
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement