github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/event/event_test.go (about)

     1  package event
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  )
    11  
    12  const (
    13  	ETMainSaidHello   = Type("main:SaidHello")
    14  	ETMainOpSucceeded = Type("main:OperationSucceeded")
    15  	ETMainOpFailed    = Type("main:OperationFailed")
    16  )
    17  
    18  func Example() {
    19  	ctx, done := context.WithCancel(context.Background())
    20  	defer done()
    21  
    22  	bus := NewBus(ctx)
    23  
    24  	makeDoneHandler := func(label string) Handler {
    25  		return func(ctx context.Context, e Event) error {
    26  			fmt.Printf("%s handler called\n", label)
    27  			return nil
    28  		}
    29  	}
    30  
    31  	bus.SubscribeTypes(makeDoneHandler("first"), ETMainSaidHello, ETMainOpSucceeded)
    32  	bus.SubscribeTypes(makeDoneHandler("second"), ETMainSaidHello)
    33  	bus.SubscribeTypes(makeDoneHandler("third"), ETMainSaidHello)
    34  
    35  	bus.Publish(ctx, ETMainSaidHello, "hello")
    36  	bus.Publish(ctx, ETMainOpSucceeded, "operation worked!")
    37  
    38  	// Output: first handler called
    39  	// second handler called
    40  	// third handler called
    41  	// first handler called
    42  }
    43  
    44  func TestEventSubscribeTypes(t *testing.T) {
    45  	ctx, done := context.WithCancel(context.Background())
    46  	defer done()
    47  
    48  	counter := 0
    49  	prevNowFunc := NowFunc
    50  	NowFunc = func() time.Time {
    51  		counter++
    52  		return time.Unix(int64(1234567000+counter), 0)
    53  	}
    54  	defer func() { NowFunc = prevNowFunc }()
    55  
    56  	bus := NewBus(ctx)
    57  
    58  	var gotNumEvents int
    59  	var gotTimestamp int64
    60  	var gotPayload interface{}
    61  	handler := func(ctx context.Context, e Event) error {
    62  		gotNumEvents++
    63  		gotTimestamp = e.Timestamp
    64  		gotPayload = e.Payload
    65  		return nil
    66  	}
    67  
    68  	bus.SubscribeTypes(handler, ETMainSaidHello)
    69  
    70  	bus.Publish(ctx, ETMainOpFailed, "ignore me")
    71  	bus.Publish(ctx, ETMainSaidHello, "hello")
    72  	bus.Publish(ctx, ETMainOpSucceeded, "ignore me too")
    73  
    74  	// Got 1 event
    75  	expectNum := 1
    76  	if diff := cmp.Diff(expectNum, gotNumEvents); diff != "" {
    77  		t.Errorf("num events (-want +got):\n%s", diff)
    78  	}
    79  	// Timestamp has 2 seconds from the initial value
    80  	expectTs := int64(1234567002000000000)
    81  	if diff := cmp.Diff(expectTs, gotTimestamp); diff != "" {
    82  		t.Errorf("timestamp (-want +got):\n%s", diff)
    83  	}
    84  	// Only type we care about sets the payload value
    85  	expectPayload := "hello"
    86  	if diff := cmp.Diff(expectPayload, gotPayload); diff != "" {
    87  		t.Errorf("payload (-want +got):\n%s", diff)
    88  	}
    89  }
    90  
    91  func TestEventSubscribeID(t *testing.T) {
    92  	ctx, done := context.WithCancel(context.Background())
    93  	defer done()
    94  
    95  	counter := 0
    96  	prevNowFunc := NowFunc
    97  	NowFunc = func() time.Time {
    98  		counter++
    99  		return time.Unix(int64(1234567000+counter), 0)
   100  	}
   101  	defer func() { NowFunc = prevNowFunc }()
   102  
   103  	bus := NewBus(ctx)
   104  
   105  	var gotNumEvents int
   106  	var gotTimestamp int64
   107  	var gotPayload interface{}
   108  	handler := func(ctx context.Context, e Event) error {
   109  		gotNumEvents++
   110  		gotTimestamp = e.Timestamp
   111  		gotPayload = e.Payload
   112  		return nil
   113  	}
   114  
   115  	bus.SubscribeID(handler, "789")
   116  
   117  	bus.PublishID(ctx, ETMainSaidHello, "123", "hi1")
   118  	bus.PublishID(ctx, ETMainSaidHello, "456", "hi2")
   119  	bus.PublishID(ctx, ETMainSaidHello, "789", "hi3")
   120  	bus.PublishID(ctx, ETMainSaidHello, "321", "hi4")
   121  
   122  	// Got 1 event
   123  	expectNum := 1
   124  	if diff := cmp.Diff(expectNum, gotNumEvents); diff != "" {
   125  		t.Errorf("num events (-want +got):\n%s", diff)
   126  	}
   127  	// Timestamp has 3 seconds from the initial value
   128  	expectTs := int64(1234567003000000000)
   129  	if diff := cmp.Diff(expectTs, gotTimestamp); diff != "" {
   130  		t.Errorf("timestamp (-want +got):\n%s", diff)
   131  	}
   132  	// Only id we care about sets the payload value
   133  	expectPayload := "hi3"
   134  	if diff := cmp.Diff(expectPayload, gotPayload); diff != "" {
   135  		t.Errorf("payload (-want +got):\n%s", diff)
   136  	}
   137  }
   138  
   139  func TestEventSubscribeAll(t *testing.T) {
   140  	ctx, done := context.WithCancel(context.Background())
   141  	defer done()
   142  
   143  	counter := 0
   144  	prevNowFunc := NowFunc
   145  	NowFunc = func() time.Time {
   146  		counter++
   147  		return time.Unix(int64(1234567000+counter), 0)
   148  	}
   149  	defer func() { NowFunc = prevNowFunc }()
   150  
   151  	bus := NewBus(ctx)
   152  
   153  	var gotNumEvents int
   154  	handler := func(ctx context.Context, e Event) error {
   155  		gotNumEvents++
   156  		return nil
   157  	}
   158  
   159  	bus.SubscribeAll(handler)
   160  
   161  	bus.Publish(ctx, ETMainOpFailed, "ignore me")
   162  	bus.Publish(ctx, ETMainSaidHello, "hello")
   163  	bus.Publish(ctx, ETMainOpSucceeded, "ignore me too")
   164  	bus.PublishID(ctx, ETMainSaidHello, "123", "hi1")
   165  
   166  	// Got all 4 events
   167  	expectNum := 4
   168  	if diff := cmp.Diff(expectNum, gotNumEvents); diff != "" {
   169  		t.Errorf("num events (-want +got):\n%s", diff)
   170  	}
   171  }