MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / TestHandleMessageSuccess

Function TestHandleMessageSuccess

lib/event/router_test.go:95–151  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

93}
94
95func TestHandleMessageSuccess(t *testing.T) {
96 r := NewRouter()
97
98 var handledV1, handledV2 bool
99 eventID1 := "event1"
100 eventID2 := "event2"
101
102 testHandler1 := func(_ context.Context, eventID string, e TestEventV1) error {
103 handledV1 = true
104 if e.ID != "v1-id" {
105 t.Errorf("v1 handler received wrong ID: %s", e.ID)
106 }
107
108 if eventID != "event1" {
109 t.Errorf("v1 handler received wrong eventID: %s", eventID)
110 }
111
112 return nil
113 }
114 testHandler2 := func(_ context.Context, eventID string, e TestEventV2) error {
115 handledV2 = true
116 if e.ID != "v2-id" || e.Priority != 5 {
117 t.Errorf("v2 handler received wrong data: %+v", e)
118 }
119
120 if eventID != "event2" {
121 t.Errorf("v2 handler received wrong eventID: %s", eventID)
122 }
123
124 return nil
125 }
126
127 // Register handlers
128 Register(r, testHandler1)
129 Register(r, testHandler2)
130
131 // Create and route V1 event
132 v1Payload, err := New(TestEventV1{ID: "v1-id"})
133 if err != nil {
134 t.Fatalf("failed to create v1 event: %v", err)
135 }
136 if err := r.HandleMessage(t.Context(), eventID1, v1Payload); err != nil {
137 t.Errorf("failed to handle v1: %v", err)
138 }
139 if !handledV1 {
140 t.Error("v1 handler was not called")
141 }
142
143 // Create and route V2 event
144 v2Payload, _ := New(TestEventV2{ID: "v2-id", Priority: 5})
145 if err := r.HandleMessage(t.Context(), eventID2, v2Payload); err != nil {
146 t.Errorf("failed to handle v2: %v", err)
147 }
148 if !handledV2 {
149 t.Error("v2 handler was not called")
150 }
151}
152

Callers

nothing calls this directly

Calls 5

HandleMessageMethod · 0.95
NewRouterFunction · 0.85
NewFunction · 0.85
RegisterFunction · 0.70
ErrorMethod · 0.45

Tested by

no test coverage detected