forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_reporter_test.go
97 lines (80 loc) · 2.54 KB
/
stat_reporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package activator
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
gorillawebsocket "github.com/gorilla/websocket"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
logtesting "knative.dev/pkg/logging/testing"
"knative.dev/serving/pkg/autoscaler/metrics"
)
func TestReportStats(t *testing.T) {
logger := logtesting.TestLogger(t)
ch := make(chan []metrics.StatMessage)
results := make(chan []byte)
sink := sendRawFunc(func(msgType int, msg []byte) error {
if msgType != gorillawebsocket.BinaryMessage {
t.Errorf("Expected metrics to be sent as Binary (%d), was %d", gorillawebsocket.BinaryMessage, msgType)
}
results <- msg
return nil
})
defer close(ch)
go ReportStats(logger, sink, ch)
inputs := [][]metrics.StatMessage{{{
Key: types.NamespacedName{Name: "first-a"},
}, {
Key: types.NamespacedName{Name: "first-b"},
}}, {{
Key: types.NamespacedName{Name: "second-a"},
}, {
Key: types.NamespacedName{Name: "second-b"},
}}}
for _, input := range inputs {
ch <- input
}
received := make(chan struct{})
output := make([][]byte, len(inputs))
go func() {
for i := 0; i < len(inputs); i++ {
output[i] = <-results
}
close(received)
}()
select {
case <-received:
var statNames []string
for _, b := range output {
var wsms metrics.WireStatMessages
if err := wsms.Unmarshal(b); err != nil {
t.Errorf("Unmarshal stats = %v, expected no error", err)
}
for _, m := range wsms.Messages {
statNames = append(statNames, m.ToStatMessage().Key.Name)
}
}
want := sets.NewString("first-a", "first-b", "second-a", "second-b")
if got := sets.NewString(statNames...); !got.Equal(want) {
t.Error("Expected to receive all stats (-want, +got):", cmp.Diff(want.List(), got.List()))
}
case <-time.After(2 * time.Second):
t.Fatal("Did not receive results after 2 seconds")
}
}
type sendRawFunc func(msgType int, msg []byte) error
func (fn sendRawFunc) SendRaw(msgType int, msg []byte) error {
return fn(msgType, msg)
}