92
}
93
}
94
95
+// Emit writes a debug line for every event, so with debug logging on an event
96
+// is observable even when nothing is subscribed. A predicate that answered
97
+// only about subscribers would silence that line.
98
+func TestShouldEmit(t *testing.T) {
99
+ app, _, cancel := testApp(t)
100
+ defer cancel()
101
+
102
+ if app.ShouldEmit("cert_obtained") {
103
+ t.Error("nothing is subscribed and debug is off; should not emit")
104
+ }
105
+
106
+ if err := app.On("cert_obtained", new(countingHandler)); err != nil {
107
+ t.Fatal(err)
108
+ }
109
+ if !app.ShouldEmit("cert_obtained") {
110
+ t.Error("a handler is bound by name; should emit")
111
+ }
112
+ if app.ShouldEmit("tls_get_certificate") {
113
+ t.Error("nothing is bound to this event; should not emit")
114
+ }
115
+}
116
+
117
+func TestShouldEmitWithCatchAllSubscriber(t *testing.T) {
118
+ app, _, cancel := testApp(t)
119
+ defer cancel()
120
+
121
+ if err := app.Subscribe(&Subscription{Handlers: []Handler{new(countingHandler)}}); err != nil {
122
+ t.Fatal(err)
123
+ }
124
+ if !app.ShouldEmit("tls_get_certificate") {
125
+ t.Error("a catch-all subscription observes every event; should emit")
126
+ }
127
+}
128
+
129
+func TestShouldEmitWithDebugLogging(t *testing.T) {
130
+ app, _, cancel := testApp(t)
131
+ defer cancel()
132
+ app.logger = zap.New(zapcore.NewCore(
133
+ zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
134
+ zapcore.AddSync(io.Discard),
135
+ zapcore.DebugLevel,
136
+ ))
137
+
138
+ if !app.ShouldEmit("tls_get_certificate") {
139
+ t.Error("debug logging observes every event; should emit")
140
+ }
141
+}
142
+
143
// Some events, such as tls_get_certificate, are emitted on every TLS
144
// handshake, whether or not anything is subscribed to them.
145
func BenchmarkEmitNoSubscribers(b *testing.B) {