Skip to content

Commit

Permalink
Adjusting tests, so they are more similar (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Wessendorf <[email protected]>
  • Loading branch information
matzew authored Feb 26, 2024
1 parent 7a58f88 commit 484581c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 59 deletions.
62 changes: 33 additions & 29 deletions cloudevents/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
63 changes: 33 additions & 30 deletions http/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 484581c

Please sign in to comment.