github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/event/pubsub_test.go (about)

     1  package event_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal/event"
    11  	"github.com/sirupsen/logrus"
    12  	logrusTest "github.com/sirupsen/logrus/hooks/test"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  	"k8s.io/apimachinery/pkg/util/wait"
    16  )
    17  
    18  func TestPubSub(t *testing.T) {
    19  	// given
    20  	var gotEventAList1 []eventA
    21  	var gotEventAList2 []eventA
    22  	var mu sync.Mutex
    23  	handlerA1 := func(ctx context.Context, ev interface{}) error {
    24  		mu.Lock()
    25  		defer mu.Unlock()
    26  		gotEventAList1 = append(gotEventAList1, ev.(eventA))
    27  		return nil
    28  	}
    29  	handlerA2 := func(ctx context.Context, ev interface{}) error {
    30  		mu.Lock()
    31  		defer mu.Unlock()
    32  		gotEventAList2 = append(gotEventAList2, ev.(eventA))
    33  		return nil
    34  	}
    35  	var gotEventBList []eventB
    36  	handlerB := func(ctx context.Context, ev interface{}) error {
    37  		mu.Lock()
    38  		defer mu.Unlock()
    39  		gotEventBList = append(gotEventBList, ev.(eventB))
    40  		return nil
    41  	}
    42  	svc := event.NewPubSub(logrus.New())
    43  	svc.Subscribe(eventA{}, handlerA1)
    44  	svc.Subscribe(eventB{}, handlerB)
    45  	svc.Subscribe(eventA{}, handlerA2)
    46  
    47  	// when
    48  	svc.Publish(context.TODO(), eventA{msg: "first event"})
    49  	svc.Publish(context.TODO(), eventB{msg: "second event"})
    50  	svc.Publish(context.TODO(), eventA{msg: "third event"})
    51  
    52  	time.Sleep(1 * time.Millisecond)
    53  
    54  	// then
    55  	assert.NoError(t, wait.PollImmediate(20*time.Millisecond, 2*time.Second, func() (bool, error) {
    56  		return containsA(gotEventAList1, eventA{msg: "first event"}) &&
    57  			containsA(gotEventAList1, eventA{msg: "third event"}) &&
    58  			containsA(gotEventAList2, eventA{msg: "first event"}) &&
    59  			containsA(gotEventAList2, eventA{msg: "third event"}) &&
    60  			containsB(gotEventBList, eventB{msg: "second event"}), nil
    61  	}))
    62  }
    63  
    64  func TestPubSub_WhenHandlerReturnsError(t *testing.T) {
    65  	// given
    66  	logger, hook := logrusTest.NewNullLogger()
    67  	var mu sync.Mutex
    68  	handlerA1 := func(ctx context.Context, ev interface{}) error {
    69  		mu.Lock()
    70  		defer mu.Unlock()
    71  		return fmt.Errorf("some error")
    72  	}
    73  	svc := event.NewPubSub(logger)
    74  	svc.Subscribe(eventA{}, handlerA1)
    75  
    76  	// when
    77  	svc.Publish(context.TODO(), eventA{msg: "first event"})
    78  
    79  	time.Sleep(1 * time.Millisecond)
    80  
    81  	// then
    82  	require.Equal(t, 1, len(hook.Entries))
    83  	require.Equal(t, hook.LastEntry().Message, "error while calling pubsub event handler: some error")
    84  }
    85  
    86  func containsA(slice []eventA, item eventA) bool {
    87  	for _, s := range slice {
    88  		if s == item {
    89  			return true
    90  		}
    91  	}
    92  	return false
    93  }
    94  
    95  func containsB(slice []eventB, item eventB) bool {
    96  	for _, s := range slice {
    97  		if s == item {
    98  			return true
    99  		}
   100  	}
   101  	return false
   102  }
   103  
   104  type eventA struct {
   105  	msg string
   106  }
   107  
   108  type eventB struct {
   109  	msg string
   110  }