github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/internal/message/sort_test.go (about) 1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package message 16 17 import ( 18 "testing" 19 "time" 20 21 "google.golang.org/protobuf/proto" 22 23 "github.com/google/fleetspeak/fleetspeak/src/client/comms" 24 "github.com/google/fleetspeak/fleetspeak/src/client/flow" 25 26 fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak" 27 ) 28 29 func TestSortLoop(t *testing.T) { 30 in := make(chan comms.MessageInfo) 31 out := make(chan comms.MessageInfo) 32 go SortLoop(in, out, flow.NewFilter()) 33 defer close(in) 34 35 for _, tc := range []struct { 36 name string 37 in []*fspb.Message 38 want []*fspb.Message 39 }{ 40 { 41 name: "LOW order doesn't change", 42 in: []*fspb.Message{ 43 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_LOW}, 44 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_LOW}, 45 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_LOW}, 46 }, 47 want: []*fspb.Message{ 48 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_LOW}, 49 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_LOW}, 50 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_LOW}, 51 }, 52 }, 53 { 54 name: "MEDIUM order doesn't change", 55 in: []*fspb.Message{ 56 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_MEDIUM}, 57 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_MEDIUM}, 58 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_MEDIUM}, 59 }, 60 want: []*fspb.Message{ 61 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_MEDIUM}, 62 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_MEDIUM}, 63 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_MEDIUM}, 64 }, 65 }, 66 { 67 name: "HIGH order doesn't change", 68 in: []*fspb.Message{ 69 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_HIGH}, 70 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_HIGH}, 71 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_HIGH}, 72 }, 73 want: []*fspb.Message{ 74 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_HIGH}, 75 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_HIGH}, 76 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_HIGH}, 77 }, 78 }, 79 { 80 name: "Sorts", 81 in: []*fspb.Message{ 82 {MessageId: []byte{0, 1, 0}, Priority: fspb.Message_LOW}, 83 {MessageId: []byte{0, 2, 0}, Priority: fspb.Message_MEDIUM}, 84 {MessageId: []byte{0, 3, 0}, Priority: fspb.Message_HIGH}, 85 {MessageId: []byte{0, 4, 0}, Priority: fspb.Message_HIGH}, 86 {MessageId: []byte{0, 5, 0}, Priority: fspb.Message_MEDIUM}, 87 {MessageId: []byte{0, 6, 0}, Priority: fspb.Message_LOW}, 88 }, 89 want: []*fspb.Message{ 90 {MessageId: []byte{0, 3, 0}, Priority: fspb.Message_HIGH}, 91 {MessageId: []byte{0, 4, 0}, Priority: fspb.Message_HIGH}, 92 {MessageId: []byte{0, 2, 0}, Priority: fspb.Message_MEDIUM}, 93 {MessageId: []byte{0, 5, 0}, Priority: fspb.Message_MEDIUM}, 94 {MessageId: []byte{0, 1, 0}, Priority: fspb.Message_LOW}, 95 {MessageId: []byte{0, 6, 0}, Priority: fspb.Message_LOW}, 96 }, 97 }, 98 } { 99 for _, m := range tc.in { 100 in <- comms.MessageInfo{M: m} 101 } 102 for _, m := range tc.want { 103 got := <-out 104 if !proto.Equal(got.M, m) { 105 t.Errorf("%s: Unexpected message from output, got %v want %v.", tc.name, got.M, m) 106 } 107 } 108 } 109 } 110 111 func TestFilter(t *testing.T) { 112 in := make(chan comms.MessageInfo) 113 out := make(chan comms.MessageInfo) 114 f := flow.NewFilter() 115 go SortLoop(in, out, f) 116 defer close(in) 117 118 for _, tc := range []struct { 119 name string 120 l, m, h bool 121 in []*fspb.Message 122 want []*fspb.Message 123 }{ 124 { 125 name: "Filter all 1", 126 l: true, 127 m: true, 128 h: true, 129 in: []*fspb.Message{ 130 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_LOW}, 131 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_MEDIUM}, 132 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_HIGH}, 133 }, 134 want: []*fspb.Message{}, 135 }, 136 { 137 name: "Filter none 1", 138 in: []*fspb.Message{}, 139 want: []*fspb.Message{ 140 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_HIGH}, 141 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_MEDIUM}, 142 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_LOW}, 143 }, 144 }, 145 { 146 name: "Filter all 2", 147 l: true, 148 m: true, 149 h: true, 150 in: []*fspb.Message{ 151 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_LOW}, 152 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_MEDIUM}, 153 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_HIGH}, 154 }, 155 want: []*fspb.Message{}, 156 }, 157 { 158 name: "Filter low", 159 l: true, 160 in: []*fspb.Message{}, 161 want: []*fspb.Message{ 162 {MessageId: []byte{0, 0, 2}, Priority: fspb.Message_HIGH}, 163 {MessageId: []byte{0, 0, 1}, Priority: fspb.Message_MEDIUM}, 164 }, 165 }, 166 { 167 name: "Filter none 2", 168 in: []*fspb.Message{}, 169 want: []*fspb.Message{ 170 {MessageId: []byte{0, 0, 0}, Priority: fspb.Message_LOW}, 171 }, 172 }, 173 } { 174 f.Set(tc.l, tc.m, tc.h) 175 for _, m := range tc.in { 176 in <- comms.MessageInfo{M: m} 177 } 178 for _, m := range tc.want { 179 got := <-out 180 if !proto.Equal(got.M, m) { 181 t.Errorf("%s: Unexpected message from output, got %v want %v.", tc.name, got.M, m) 182 } 183 } 184 select { 185 case got := <-out: 186 t.Errorf("%s: Expected no more results, got %v", tc.name, got.M) 187 case <-time.After(time.Second): 188 } 189 } 190 }