MCPcopy
hub / github.com/aws/aws-lambda-go / TestHandlerTrace

Function TestHandlerTrace

lambda/handler_test.go:462–521  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

460}
461
462func TestHandlerTrace(t *testing.T) {
463 handler := NewHandler(func(ctx context.Context, x int) (int, error) {
464 if x != 123 {
465 t.Error(x)
466 }
467 return 456, nil
468 })
469 requestHistory := ""
470 responseHistory := ""
471 checkInt := func(e interface{}, expected int) {
472 nt, ok := e.(int)
473 if !ok {
474 t.Error("not int as expected", e)
475 return
476 }
477 if nt != expected {
478 t.Error("unexpected value", nt, expected)
479 }
480 }
481 ctx := context.Background()
482 ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{}) // empty HandlerTrace
483 ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{ // with RequestEvent
484 RequestEvent: func(c context.Context, e interface{}) {
485 requestHistory += "A"
486 checkInt(e, 123)
487 },
488 })
489 ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{ // with ResponseEvent
490 ResponseEvent: func(c context.Context, e interface{}) {
491 responseHistory += "X"
492 checkInt(e, 456)
493 },
494 })
495 ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{ // with RequestEvent and ResponseEvent
496 RequestEvent: func(c context.Context, e interface{}) {
497 requestHistory += "B"
498 checkInt(e, 123)
499 },
500 ResponseEvent: func(c context.Context, e interface{}) {
501 responseHistory += "Y"
502 checkInt(e, 456)
503 },
504 })
505 ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{}) // empty HandlerTrace
506
507 payload := []byte(`123`)
508 js, err := handler.Invoke(ctx, payload)
509 if err != nil {
510 t.Error("unexpected handler error", err)
511 }
512 if string(js) != "456" {
513 t.Error("unexpected handler output", string(js))
514 }
515 if requestHistory != "AB" {
516 t.Error("request callbacks not called as expected", requestHistory)
517 }
518 if responseHistory != "XY" {
519 t.Error("response callbacks not called as expected", responseHistory)

Callers

nothing calls this directly

Calls 4

InvokeMethod · 0.95
NewContextFunction · 0.92
NewHandlerFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…