diff --git a/cloudevents/service_test.go b/cloudevents/service_test.go index e64f9c2..2adf686 100644 --- a/cloudevents/service_test.go +++ b/cloudevents/service_test.go @@ -244,24 +244,26 @@ func TestStop_Invoked(t *testing.T) { func TestHandle_Invoked(t *testing.T) { t.Setenv("LISTEN_ADDRESS", "127.0.0.1:") // use an OS-chosen port - errCh := make(chan error) - startCh := make(chan any) - timeoutCh := time.After(500 * time.Millisecond) - - f := &mock.Function{ - OnStart: func(_ context.Context, _ map[string]string) error { + var ( + ctx, cancel = context.WithCancel(context.Background()) + errCh = make(chan error) + startCh = make(chan any) + timeoutCh = time.After(500 * time.Millisecond) + onStart = func(_ context.Context, _ map[string]string) error { startCh <- true return nil - }, - OnHandle: func(ctx context.Context, event event.Event) (*event.Event, error) { + } + onHandle = func(_ context.Context, event event.Event) (*event.Event, error) { fmt.Println("Instanced CloudEvents handler invoked") fmt.Println(event) // echo to local output return nil, nil // echo to caller - }} + } + ) + defer cancel() + f := &mock.Function{OnStart: onStart, OnHandle: onHandle} service := New(f) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + go func() { if err := service.Start(ctx); err != nil { errCh <- err @@ -304,19 +306,20 @@ func TestHandle_Invoked(t *testing.T) { func TestReady_Invoked(t *testing.T) { t.Setenv("LISTEN_ADDRESS", "127.0.0.1:") // use an OS-chosen port - errCh := make(chan error) - startCh := make(chan any) - timeoutCh := time.After(500 * time.Millisecond) - - f := &mock.Function{ - OnStart: func(_ context.Context, _ map[string]string) error { + var ( + ctx, cancel = context.WithCancel(context.Background()) + errCh = make(chan error) + startCh = make(chan any) + timeoutCh = time.After(500 * time.Millisecond) + onStart = func(_ context.Context, _ map[string]string) error { startCh <- true return nil - }} + } + ) + defer cancel() + f := &mock.Function{OnStart: onStart} service := New(f) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() go func() { if err := service.Start(ctx); err != nil { errCh <- err @@ -350,19 +353,20 @@ func TestReady_Invoked(t *testing.T) { func TestAlive_Invoked(t *testing.T) { t.Setenv("LISTEN_ADDRESS", "127.0.0.1:") // use an OS-chosen port - errCh := make(chan error) - startCh := make(chan any) - timeoutCh := time.After(500 * time.Millisecond) - - f := &mock.Function{ - OnStart: func(_ context.Context, _ map[string]string) error { + var ( + ctx, cancel = context.WithCancel(context.Background()) + errCh = make(chan error) + startCh = make(chan any) + timeoutCh = time.After(500 * time.Millisecond) + onStart = func(_ context.Context, _ map[string]string) error { startCh <- true return nil - }} + } + ) + defer cancel() + f := &mock.Function{OnStart: onStart} service := New(f) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() go func() { if err := service.Start(ctx); err != nil { errCh <- err diff --git a/http/service_test.go b/http/service_test.go index df02090..79b3ab0 100644 --- a/http/service_test.go +++ b/http/service_test.go @@ -248,23 +248,24 @@ func TestStop_Invoked(t *testing.T) { // a successful http request. func TestHandle_Invoked(t *testing.T) { t.Setenv("LISTEN_ADDRESS", "127.0.0.1:") // use an OS-chosen port - - errCh := make(chan error) - startCh := make(chan any) - timeoutCh := time.After(500 * time.Millisecond) - - f := &mock.Function{ - OnStart: func(_ context.Context, _ map[string]string) error { + var ( + ctx, cancel = context.WithCancel(context.Background()) + errCh = make(chan error) + startCh = make(chan any) + timeoutCh = time.After(500 * time.Millisecond) + onStart = func(_ context.Context, _ map[string]string) error { startCh <- true return nil - }, - OnHandle: func(_ context.Context, w http.ResponseWriter, _ *http.Request) { + } + onHandle = func(_ context.Context, w http.ResponseWriter, _ *http.Request) { fmt.Fprintf(w, "OK") - }} + } + ) + defer cancel() + f := &mock.Function{OnStart: onStart, OnHandle: onHandle} service := New(f) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + go func() { if err := service.Start(ctx); err != nil { errCh <- err @@ -305,19 +306,20 @@ func TestHandle_Invoked(t *testing.T) { func TestReady_Invoked(t *testing.T) { t.Setenv("LISTEN_ADDRESS", "127.0.0.1:") // use an OS-chosen port - errCh := make(chan error) - startCh := make(chan any) - timeoutCh := time.After(500 * time.Millisecond) - - f := &mock.Function{ - OnStart: func(_ context.Context, _ map[string]string) error { + var ( + ctx, cancel = context.WithCancel(context.Background()) + errCh = make(chan error) + startCh = make(chan any) + timeoutCh = time.After(500 * time.Millisecond) + onStart = func(_ context.Context, _ map[string]string) error { startCh <- true return nil - }} + } + ) + defer cancel() + f := &mock.Function{OnStart: onStart} service := New(f) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() go func() { if err := service.Start(ctx); err != nil { errCh <- err @@ -351,19 +353,20 @@ func TestReady_Invoked(t *testing.T) { func TestAlive_Invoked(t *testing.T) { t.Setenv("LISTEN_ADDRESS", "127.0.0.1:") // use an OS-chosen port - errCh := make(chan error) - startCh := make(chan any) - timeoutCh := time.After(500 * time.Millisecond) - - f := &mock.Function{ - OnStart: func(_ context.Context, _ map[string]string) error { + var ( + ctx, cancel = context.WithCancel(context.Background()) + errCh = make(chan error) + startCh = make(chan any) + timeoutCh = time.After(500 * time.Millisecond) + onStart = func(_ context.Context, _ map[string]string) error { startCh <- true return nil - }} + } + ) + defer cancel() + f := &mock.Function{OnStart: onStart} service := New(f) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() go func() { if err := service.Start(ctx); err != nil { errCh <- err