|
| 1 | +package labels |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/square/p2/pkg/logging" |
| 8 | + |
| 9 | + . "github.com/square/p2/Godeps/_workspace/src/github.com/anthonybishopric/gotcha" |
| 10 | + "github.com/square/p2/Godeps/_workspace/src/k8s.io/kubernetes/pkg/labels" |
| 11 | +) |
| 12 | + |
| 13 | +func alterAggregationTime(dur time.Duration) { |
| 14 | + AggregationRateCap = dur |
| 15 | +} |
| 16 | + |
| 17 | +func fakeLabeledPods() map[string][]byte { |
| 18 | + return map[string][]byte{ |
| 19 | + objectPath(POD, "maroono"): []byte(`{"color": "red", "deployment": "production"}`), |
| 20 | + objectPath(POD, "emeralda"): []byte(`{"color": "green", "deployment": "canary"}`), |
| 21 | + objectPath(POD, "slashi"): []byte(`{"color": "red", "deployment": "canary"}`), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Check that two clients can share an aggregator |
| 26 | +func TestTwoClients(t *testing.T) { |
| 27 | + alterAggregationTime(time.Millisecond) |
| 28 | + |
| 29 | + fakeKV := &fakeLabelStore{fakeLabeledPods(), nil} |
| 30 | + aggreg := NewConsulAggregator(POD, fakeKV, logging.DefaultLogger) |
| 31 | + go aggreg.Aggregate() |
| 32 | + defer aggreg.Quit() |
| 33 | + |
| 34 | + quitCh := make(chan struct{}) |
| 35 | + labeledChannel1 := aggreg.Watch(labels.Everything().Add("color", labels.EqualsOperator, []string{"green"}), quitCh) |
| 36 | + labeledChannel2 := aggreg.Watch(labels.Everything().Add("deployment", labels.EqualsOperator, []string{"canary"}), quitCh) |
| 37 | + |
| 38 | + var checked string |
| 39 | + for i := 0; i < 2; i++ { |
| 40 | + select { |
| 41 | + case <-time.After(time.Second): |
| 42 | + t.Fatal("Should not have taken a second to get results") |
| 43 | + case labeledPtr := <-labeledChannel1: |
| 44 | + Assert(t).IsNotNil(labeledPtr, "ptr should not have been nil") |
| 45 | + labeled := *labeledPtr |
| 46 | + Assert(t).AreNotEqual("green", checked, "Should not have already checked the green selector result") |
| 47 | + checked = "green" // ensure that both sides get checked |
| 48 | + Assert(t).AreEqual(1, len(labeled), "Should have received one result from the color watch") |
| 49 | + Assert(t).AreEqual("emeralda", labeled[0].ID, "should have received the emerald app") |
| 50 | + case labeledPtr := <-labeledChannel2: |
| 51 | + Assert(t).IsNotNil(labeledPtr, "ptr should not have been nil") |
| 52 | + labeled := *labeledPtr |
| 53 | + Assert(t).AreNotEqual("canary", checked, "Should not have already checked the canary selector result") |
| 54 | + checked = "canary" // ensure that both sides get checked |
| 55 | + Assert(t).AreEqual(2, len(labeled), "Should have received two results from the canary watch") |
| 56 | + emeraldaIndex := 0 |
| 57 | + slashiIndex := 1 |
| 58 | + if labeled[0].ID == "slashi" { // order doesn't matter |
| 59 | + emeraldaIndex, slashiIndex = slashiIndex, emeraldaIndex |
| 60 | + } |
| 61 | + Assert(t).AreEqual("emeralda", labeled[emeraldaIndex].ID, "should have received the emerald app") |
| 62 | + Assert(t).AreEqual("slashi", labeled[slashiIndex].ID, "should have received the slashi app") |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestQuitAggregateAfterResults(t *testing.T) { |
| 68 | + alterAggregationTime(time.Millisecond) |
| 69 | + |
| 70 | + fakeKV := &fakeLabelStore{fakeLabeledPods(), nil} |
| 71 | + aggreg := NewConsulAggregator(POD, fakeKV, logging.DefaultLogger) |
| 72 | + go aggreg.Aggregate() |
| 73 | + |
| 74 | + quitCh := make(chan struct{}) |
| 75 | + res := aggreg.Watch(labels.Everything().Add("color", labels.EqualsOperator, []string{"green"}), quitCh) |
| 76 | + |
| 77 | + // Quit now. We expect that the aggregator will close the res channels |
| 78 | + aggreg.Quit() |
| 79 | + success := make(chan struct{}) |
| 80 | + go func() { |
| 81 | + for _ = range res { |
| 82 | + } |
| 83 | + success <- struct{}{} |
| 84 | + }() |
| 85 | + |
| 86 | + select { |
| 87 | + case <-success: |
| 88 | + case <-time.After(time.Second): |
| 89 | + t.Fatal("Should not be waiting or processing results after a second") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestQuitAggregateBeforeResults(t *testing.T) { |
| 94 | + alterAggregationTime(time.Millisecond) |
| 95 | + |
| 96 | + // this channel prevents the List from returning, so the aggregator |
| 97 | + // must quit prior to entering the loop |
| 98 | + trigger := make(chan struct{}) |
| 99 | + fakeKV := &fakeLabelStore{fakeLabeledPods(), trigger} |
| 100 | + aggreg := NewConsulAggregator(POD, fakeKV, logging.DefaultLogger) |
| 101 | + go aggreg.Aggregate() |
| 102 | + |
| 103 | + quitCh := make(chan struct{}) |
| 104 | + res := aggreg.Watch(labels.Everything().Add("color", labels.EqualsOperator, []string{"green"}), quitCh) |
| 105 | + |
| 106 | + // Quit now. We expect that the aggregator will close the res channels |
| 107 | + aggreg.Quit() |
| 108 | + |
| 109 | + select { |
| 110 | + case labeled := <-res: |
| 111 | + Assert(t).IsNotNil(labeled, "Should not have received any results") |
| 112 | + case <-time.After(time.Second): |
| 113 | + t.Fatal("Should still be waiting or processing results after a second") |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestQuitIndividualWatch(t *testing.T) { |
| 118 | + alterAggregationTime(time.Millisecond) |
| 119 | + |
| 120 | + fakeKV := &fakeLabelStore{fakeLabeledPods(), nil} |
| 121 | + aggreg := NewConsulAggregator(POD, fakeKV, logging.DefaultLogger) |
| 122 | + go aggreg.Aggregate() |
| 123 | + |
| 124 | + quitCh1 := make(chan struct{}) |
| 125 | + labeledChannel1 := aggreg.Watch(labels.Everything().Add("color", labels.EqualsOperator, []string{"green"}), quitCh1) |
| 126 | + |
| 127 | + quitCh2 := make(chan struct{}) |
| 128 | + labeledChannel2 := aggreg.Watch(labels.Everything().Add("deployment", labels.EqualsOperator, []string{"production"}), quitCh2) |
| 129 | + |
| 130 | + close(quitCh1) // this should not interrupt the flow of messages to the second channel |
| 131 | + |
| 132 | + // iterate twice to show that we are not waiting on other now-closed channels |
| 133 | + for i := 0; i < 2; i++ { |
| 134 | + select { |
| 135 | + case <-time.After(time.Second): |
| 136 | + t.Fatalf("Should not have taken a second to get results on iteration %v", i) |
| 137 | + case labeledPtr := <-labeledChannel2: |
| 138 | + Assert(t).IsNotNil(labeledPtr, "ptr should not have been nil") |
| 139 | + labeled := *labeledPtr |
| 140 | + Assert(t).AreEqual(1, len(labeled), "Should have one result with a production deployment") |
| 141 | + Assert(t).AreEqual("maroono", labeled[0].ID, "Should have received maroono as the one production deployment") |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // drain the first channel to show that it was closed. We do this |
| 146 | + // in a loop since it is possible that a value was sent on the channel |
| 147 | + success := make(chan struct{}) |
| 148 | + go func() { |
| 149 | + for _ = range labeledChannel1 { |
| 150 | + } |
| 151 | + success <- struct{}{} |
| 152 | + }() |
| 153 | + select { |
| 154 | + case <-time.After(time.Second): |
| 155 | + t.Fatal("Should not have taken a second to see the closed label channel") |
| 156 | + case <-success: |
| 157 | + } |
| 158 | +} |
0 commit comments