|
| 1 | +// Copyright Envoy Gateway Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// The full text of the Apache license is available in the LICENSE file at |
| 4 | +// the root of the repo. |
| 5 | + |
| 6 | +package message |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/telepresenceio/watchable" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCoalesceUpdates(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + input []watchable.Update[string, int] |
| 21 | + expected []watchable.Update[string, int] |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "empty input returns nil", |
| 25 | + input: []watchable.Update[string, int]{}, |
| 26 | + expected: []watchable.Update[string, int]{}, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "simple updates without repeats", |
| 30 | + input: []watchable.Update[string, int]{ |
| 31 | + {Key: "foo", Value: 1}, |
| 32 | + {Key: "bar", Value: 2}, |
| 33 | + {Key: "baz", Value: 3}, |
| 34 | + }, |
| 35 | + expected: []watchable.Update[string, int]{ |
| 36 | + {Key: "foo", Value: 1}, |
| 37 | + {Key: "bar", Value: 2}, |
| 38 | + {Key: "baz", Value: 3}, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "latest update per key wins", |
| 43 | + input: []watchable.Update[string, int]{ |
| 44 | + {Key: "foo", Value: 1}, |
| 45 | + {Key: "bar", Delete: true, Value: 2}, |
| 46 | + {Key: "baz", Value: 3}, |
| 47 | + {Key: "bar", Value: 4}, |
| 48 | + {Key: "foo", Value: 5}, |
| 49 | + {Key: "baz", Delete: true, Value: 6}, |
| 50 | + {Key: "bar", Value: 7}, |
| 51 | + }, |
| 52 | + expected: []watchable.Update[string, int]{ |
| 53 | + {Key: "foo", Value: 5}, |
| 54 | + {Key: "baz", Delete: true, Value: 6}, |
| 55 | + {Key: "bar", Value: 7}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tc := range tests { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + actual := coalesceUpdates("test-runner", tc.input) |
| 65 | + require.Equal(t, tc.expected, actual) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments