Exemple #1
0
func TestEventBus(t *testing.T) {
	bus := NewEventBus()
	if bus == nil {
		t.Fatal("there should be a bus")
	}

	observer := testutil.NewMockEventObserver()
	bus.AddObserver(observer)

	t.Log("publish event without handler")
	event1 := &testutil.TestEvent{eh.NewUUID(), "event1"}
	bus.PublishEvent(event1)
	if !reflect.DeepEqual(observer.Events, []eh.Event{event1}) {
		t.Error("the observed events should be correct:", observer.Events)
	}

	t.Log("publish event")
	handler := testutil.NewMockEventHandler("testHandler")
	bus.AddHandler(handler, testutil.TestEventType)
	bus.PublishEvent(event1)
	if !reflect.DeepEqual(handler.Events, []eh.Event{event1}) {
		t.Error("the handler events should be correct:", handler.Events)
	}
	if !reflect.DeepEqual(observer.Events, []eh.Event{event1, event1}) {
		t.Error("the observed events should be correct:", observer.Events)
	}

	t.Log("publish another event")
	bus.AddHandler(handler, testutil.TestEventOtherType)
	event2 := &testutil.TestEventOther{eh.NewUUID(), "event2"}
	bus.PublishEvent(event2)
	if !reflect.DeepEqual(handler.Events, []eh.Event{event1, event2}) {
		t.Error("the handler events should be correct:", handler.Events)
	}
	if !reflect.DeepEqual(observer.Events, []eh.Event{event1, event1, event2}) {
		t.Error("the observed events should be correct:", observer.Events)
	}
}
Exemple #2
0
func TestEventBus(t *testing.T) {
	// Support Wercker testing with MongoDB.
	host := os.Getenv("REDIS_PORT_6379_TCP_ADDR")
	port := os.Getenv("REDIS_PORT_6379_TCP_PORT")

	url := ":6379"
	if host != "" && port != "" {
		url = host + ":" + port
	}

	bus, err := NewEventBus("test", url, "")
	if err != nil {
		t.Fatal("there should be no error:", err)
	}
	if bus == nil {
		t.Fatal("there should be a bus")
	}
	defer bus.Close()
	observer := testutil.NewMockEventObserver()
	bus.AddObserver(observer)

	// Another bus to test the observer.
	bus2, err := NewEventBus("test", url, "")
	if err != nil {
		t.Fatal("there should be no error:", err)
	}
	defer bus2.Close()
	observer2 := testutil.NewMockEventObserver()
	bus2.AddObserver(observer2)

	t.Log("publish event without handler")
	event1 := &testutil.TestEvent{eh.NewUUID(), "event1"}
	bus.PublishEvent(event1)
	<-observer.Recv
	if !reflect.DeepEqual(observer.Events, []eh.Event{event1}) {
		t.Error("the observed events should be correct:", observer.Events)
	}
	<-observer2.Recv
	if !reflect.DeepEqual(observer2.Events, []eh.Event{event1}) {
		t.Error("the second observed events should be correct:", observer2.Events)
	}

	t.Log("publish event")
	handler := testutil.NewMockEventHandler("testHandler")
	bus.AddHandler(handler, testutil.TestEventType)
	bus.PublishEvent(event1)
	if !reflect.DeepEqual(handler.Events, []eh.Event{event1}) {
		t.Error("the handler events should be correct:", handler.Events)
	}
	<-observer.Recv
	if !reflect.DeepEqual(observer.Events, []eh.Event{event1, event1}) {
		t.Error("the observed events should be correct:", observer.Events)
	}
	<-observer2.Recv
	if !reflect.DeepEqual(observer2.Events, []eh.Event{event1, event1}) {
		t.Error("the second observed events should be correct:", observer2.Events)
	}

	t.Log("publish another event")
	bus.AddHandler(handler, testutil.TestEventOtherType)
	event2 := &testutil.TestEventOther{eh.NewUUID(), "event2"}
	bus.PublishEvent(event2)
	if !reflect.DeepEqual(handler.Events, []eh.Event{event1, event2}) {
		t.Error("the handler events should be correct:", handler.Events)
	}
	<-observer.Recv
	if !reflect.DeepEqual(observer.Events, []eh.Event{event1, event1, event2}) {
		t.Error("the observed events should be correct:", observer.Events)
	}
	<-observer2.Recv
	if !reflect.DeepEqual(observer2.Events, []eh.Event{event1, event1, event2}) {
		t.Error("the second observed events should be correct:", observer2.Events)
	}
}