8
8
"context"
9
9
"encoding/json"
10
10
"errors"
11
+ "io"
11
12
"net/http"
13
+ "os"
12
14
"strings"
13
15
"testing"
14
16
@@ -45,14 +47,23 @@ func distributedTracingEnabled(key string) string {
45
47
}
46
48
}
47
49
50
+ // bufWriterProvider is a testing implementation of writerProvider
51
+ type bufWriterProvider struct {
52
+ buf io.Writer
53
+ }
54
+
55
+ func (bw bufWriterProvider ) borrowWriter (needsWriter func (writer io.Writer )) {
56
+ needsWriter (bw .buf )
57
+ }
58
+
48
59
func TestColdStart (t * testing.T ) {
49
60
originalHandler := func (c context.Context ) {}
50
61
app := testApp (nil , t )
51
62
wrapped := Wrap (originalHandler , app )
52
63
w := wrapped .(* wrappedHandler )
53
64
w .functionName = "functionName"
54
65
buf := & bytes.Buffer {}
55
- w .writer = buf
66
+ w .hasWriter = bufWriterProvider { buf }
56
67
57
68
ctx := context .Background ()
58
69
lctx := & lambdacontext.LambdaContext {
@@ -104,7 +115,7 @@ func TestColdStart(t *testing.T) {
104
115
105
116
// Invoke the handler again to test the cold-start attribute absence.
106
117
buf = & bytes.Buffer {}
107
- w .writer = buf
118
+ w .hasWriter = bufWriterProvider { buf }
108
119
internal .HarvestTesting (app .Private , nil )
109
120
resp , err = wrapped .Invoke (ctx , nil )
110
121
if nil != err || string (resp ) != "null" {
@@ -154,7 +165,7 @@ func TestErrorCapture(t *testing.T) {
154
165
w := wrapped .(* wrappedHandler )
155
166
w .functionName = "functionName"
156
167
buf := & bytes.Buffer {}
157
- w .writer = buf
168
+ w .hasWriter = bufWriterProvider { buf }
158
169
159
170
resp , err := wrapped .Invoke (context .Background (), nil )
160
171
if err != returnError || string (resp ) != "" {
@@ -229,7 +240,7 @@ func TestSetWebRequest(t *testing.T) {
229
240
w := wrapped .(* wrappedHandler )
230
241
w .functionName = "functionName"
231
242
buf := & bytes.Buffer {}
232
- w .writer = buf
243
+ w .hasWriter = bufWriterProvider { buf }
233
244
234
245
req := events.APIGatewayProxyRequest {
235
246
Headers : map [string ]string {
@@ -301,7 +312,7 @@ func TestDistributedTracing(t *testing.T) {
301
312
w := wrapped .(* wrappedHandler )
302
313
w .functionName = "functionName"
303
314
buf := & bytes.Buffer {}
304
- w .writer = buf
315
+ w .hasWriter = bufWriterProvider { buf }
305
316
306
317
dtHdr := http.Header {}
307
318
app .StartTransaction ("hello" ).InsertDistributedTraceHeaders (dtHdr )
@@ -396,7 +407,7 @@ func TestEventARN(t *testing.T) {
396
407
w := wrapped .(* wrappedHandler )
397
408
w .functionName = "functionName"
398
409
buf := & bytes.Buffer {}
399
- w .writer = buf
410
+ w .hasWriter = bufWriterProvider { buf }
400
411
401
412
req := events.DynamoDBEvent {
402
413
Records : []events.DynamoDBEventRecord {{
@@ -473,7 +484,7 @@ func TestAPIGatewayProxyResponse(t *testing.T) {
473
484
w := wrapped .(* wrappedHandler )
474
485
w .functionName = "functionName"
475
486
buf := & bytes.Buffer {}
476
- w .writer = buf
487
+ w .hasWriter = bufWriterProvider { buf }
477
488
478
489
resp , err := wrapped .Invoke (context .Background (), nil )
479
490
if nil != err {
@@ -535,7 +546,7 @@ func TestCustomEvent(t *testing.T) {
535
546
w := wrapped .(* wrappedHandler )
536
547
w .functionName = "functionName"
537
548
buf := & bytes.Buffer {}
538
- w .writer = buf
549
+ w .hasWriter = bufWriterProvider { buf }
539
550
540
551
resp , err := wrapped .Invoke (context .Background (), nil )
541
552
if nil != err || string (resp ) != "null" {
@@ -555,3 +566,30 @@ func TestCustomEvent(t *testing.T) {
555
566
t .Error ("no output written" )
556
567
}
557
568
}
569
+
570
+ func TestDefaultWriterProvider (t * testing.T ) {
571
+ dwp := defaultWriterProvider {}
572
+ dwp .borrowWriter (func (writer io.Writer ) {
573
+ if writer != os .Stdout {
574
+ t .Error ("Expected stdout" )
575
+ }
576
+ })
577
+
578
+ const telemetryFile = "/tmp/newrelic-telemetry"
579
+ defer os .Remove (telemetryFile )
580
+ file , err := os .Create (telemetryFile )
581
+ if err != nil {
582
+ t .Error ("Unexpected error creating telemetry file" , err )
583
+ }
584
+
585
+ err = file .Close ()
586
+ if err != nil {
587
+ t .Error ("Error closing telemetry file" , err )
588
+ }
589
+
590
+ dwp .borrowWriter (func (writer io.Writer ) {
591
+ if writer == os .Stdout {
592
+ t .Error ("Expected telemetry file, got stdout" )
593
+ }
594
+ })
595
+ }
0 commit comments