-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventbus_test.go
More file actions
100 lines (88 loc) · 3.02 KB
/
eventbus_test.go
File metadata and controls
100 lines (88 loc) · 3.02 KB
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
98
99
100
package devcontainer
import (
"testing"
"time"
"github.com/crunchloop/devcontainer/events"
"github.com/crunchloop/devcontainer/runtime"
)
// TestBuildCompletedEvent_DurationMs verifies that the bus stamps a real
// wall-clock duration on BuildCompletedEvent. Regression guard for the
// case where DurationMs was always 0 because the field was never set in
// translateBuildEvent.
func TestBuildCompletedEvent_DurationMs(t *testing.T) {
out := make(chan events.Event, 16)
bus := newEventBus(events.NewEmitter(nil), out)
ch := bus.BuildChan(events.BuildSourceUIDReconcile)
time.Sleep(20 * time.Millisecond)
ch <- runtime.BuildEvent{Kind: runtime.BuildEventCompleted, Digest: "sha256:abc"}
bus.Close()
close(out)
var done *events.BuildCompletedEvent
for ev := range out {
if e, ok := ev.(events.BuildCompletedEvent); ok {
done = &e
}
}
if done == nil {
t.Fatal("no BuildCompletedEvent emitted")
return
}
if done.DurationMs < 15 {
t.Errorf("DurationMs = %d, want >= 15 (slept 20ms before completion)", done.DurationMs)
}
if done.Source != events.BuildSourceUIDReconcile {
t.Errorf("Source = %q, want %q", done.Source, events.BuildSourceUIDReconcile)
}
}
// TestBuildCompletedEvent_DurationMs_ResetsPerSource verifies that
// successive BuildChan(source) calls on the same bus measure each build
// independently: the second build's duration is not inflated by the
// first build's elapsed time.
func TestBuildCompletedEvent_DurationMs_ResetsPerSource(t *testing.T) {
out := make(chan events.Event, 32)
bus := newEventBus(events.NewEmitter(nil), out)
ch1 := bus.BuildChan(events.BuildSourceDockerfile)
time.Sleep(50 * time.Millisecond)
ch1 <- runtime.BuildEvent{Kind: runtime.BuildEventCompleted, Digest: "sha256:first"}
// Drain so the translator has processed the first completion before
// we reset the start timer for the second build.
waitForCompleted(t, out, "sha256:first")
bus.BuildChan(events.BuildSourceFeatures)
time.Sleep(10 * time.Millisecond)
ch1 <- runtime.BuildEvent{Kind: runtime.BuildEventCompleted, Digest: "sha256:second"}
bus.Close()
close(out)
second := drainCompleted(out, "sha256:second")
if second == nil {
t.Fatal("no second BuildCompletedEvent emitted")
return
}
if second.DurationMs >= 50 {
t.Errorf("second DurationMs = %d, expected < 50 (start should have reset)", second.DurationMs)
}
if second.Source != events.BuildSourceFeatures {
t.Errorf("second Source = %q, want %q", second.Source, events.BuildSourceFeatures)
}
}
func waitForCompleted(t *testing.T, out chan events.Event, digest string) {
t.Helper()
deadline := time.After(time.Second)
for {
select {
case ev := <-out:
if e, ok := ev.(events.BuildCompletedEvent); ok && e.ImageID == digest {
return
}
case <-deadline:
t.Fatalf("timed out waiting for BuildCompletedEvent %s", digest)
}
}
}
func drainCompleted(out chan events.Event, digest string) *events.BuildCompletedEvent {
for ev := range out {
if e, ok := ev.(events.BuildCompletedEvent); ok && e.ImageID == digest {
return &e
}
}
return nil
}