Пример #1
0
func StartDeliveryAgent() error {
	a := &DeliveryAgent{
		id:            ebus.Id("Delivery"),
		openOrders:    make(map[int]bool),
		receivedTopic: "OrderReceived",
		packedTopic:   "OrderPacked",
	}
	_, err := ebus.Register(a)
	if err != nil {
		return err
	}
	ebus.Subscribe(a, a.receivedTopic)
	ebus.Subscribe(a, a.packedTopic)
	return nil
}
Пример #2
0
func StartManufacturerAgent() error {
	a := &ManufacturerAgent{
		id:                ebus.Id("Manufacturer"),
		openOrders:        make(map[int]int),
		orderedTopic:      "ManufacturerItemOrdered",
		manufacturedTopic: "OrderedItemsManufactured",
	}
	_, err := ebus.Register(a)
	if err != nil {
		return err
	}
	ebus.AddTicker("Manufacturing", time.Second, a.manufacturedTopic)
	ebus.Subscribe(a, a.orderedTopic)
	ebus.Subscribe(a, a.manufacturedTopic)
	return nil
}
Пример #3
0
// TestTicker tests the usage of tickers.
func TestTicker(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	config := config.New(provider)

	config.Set("backend", "single")

	err := ebus.Init(config)
	assert.Nil(err, "single node backend started")

	testAgent := ebus.NewTestAgent(1)
	_, err = ebus.Register(testAgent)
	assert.Nil(err, "test agent registered")
	err = ebus.Subscribe(testAgent, "tick", "tock")

	logAgent := ebus.NewLogAgent("logger")
	_, err = ebus.Register(logAgent)
	assert.Nil(err, "log agent registered")
	err = ebus.Subscribe(logAgent, "tick")

	err = ebus.AddTicker("foo", 100*time.Millisecond, "tick", "tock")
	assert.Nil(err, "ticker foo added")
	err = ebus.AddTicker("bar", 500*time.Millisecond, "tock")
	assert.Nil(err, "ticker bar added")

	time.Sleep(1050 * time.Millisecond)
	assert.Equal(testAgent.Counters["tick"], 10, "got all ticks")
	assert.Equal(testAgent.Counters["tock"], 12, "got all tocks")

	err = ebus.AddTicker("foo", 100*time.Millisecond, "tick", "tock")
	assert.True(ebus.IsDuplicateTickerError(err), "can't add ticker twice")

	err = ebus.RemoveTicker("bar")
	assert.Nil(err, "ticker bar removed")

	err = ebus.RemoveTicker("bar")
	assert.True(ebus.IsTickerNotFoundError(err), "ticker bar is already removed")

	err = ebus.Stop()
	assert.Nil(err, "ebus stopped")

	err = ebus.RemoveTicker("foo")
	assert.True(ebus.IsTickerNotFoundError(err), "ticker foo is removed by ebus stopping")
}
Пример #4
0
func StartWarehouseAgent() error {
	a := &WarehouseAgent{
		id:            ebus.Id("Warehouse"),
		inventory:     make(map[int]int),
		openOrders:    make(map[int]map[int]OrderItem),
		orderedTopic:  "WarehouseItemOrdered",
		shippedTopic:  "ManufacturingShipped",
		preparedTopic: "OrderItemsPrepared",
	}
	_, err := ebus.Register(a)
	if err != nil {
		return err
	}
	ebus.AddTicker("Warehouse", time.Second, a.preparedTopic)
	ebus.Subscribe(a, a.orderedTopic)
	ebus.Subscribe(a, a.shippedTopic)
	ebus.Subscribe(a, a.preparedTopic)
	return nil
}
Пример #5
0
func StartWaitAgent(done chan bool) error {
	a := &WaitAgent{
		id:             ebus.Id("Wait"),
		done:           done,
		deliveredTopic: "OrderDelivered",
	}
	_, err := ebus.Register(a)
	if err != nil {
		return err
	}
	ebus.Subscribe(a, a.deliveredTopic)
	return nil
}
Пример #6
0
func StartOrderAgent(order Order) error {
	a := &OrderAgent{
		id:           ebus.Id("Order", order.OrderNo),
		order:        order,
		openItems:    make(map[int]bool),
		shippedTopic: ebus.Id("WarehouseShipped", order.OrderNo),
	}
	_, err := ebus.Register(a)
	if err != nil {
		return err
	}
	ebus.Subscribe(a, a.shippedTopic)
	// Emit a request for each order idem.
	for _, item := range order.OrderItems {
		a.openItems[item.ItemNo] = true
		ebus.Emit(item, "WarehouseItemOrdered")
	}
	return nil
}
Пример #7
0
// TestSimpleSingle the single node backend.
func TestSimpleSingle(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	config := config.New(provider)

	config.Set("backend", "single")

	err := ebus.Init(config)
	assert.Nil(err, "single node backend started")
	defer ebus.Stop()

	applog.Debugf("registering agents")
	agent1 := ebus.NewTestAgent(1)
	_, err = ebus.Register(agent1)
	assert.Nil(err, "registered agent 1")
	agent2 := ebus.NewTestAgent(2)
	_, err = ebus.Register(agent2)
	assert.Nil(err, "registered agent 2")
	agent3 := ebus.NewTestAgent(3)
	_, err = ebus.Register(agent3)
	assert.Nil(err, "registered agent 3")
	agent4 := ebus.NewTestAgent(4)
	_, err = ebus.Register(agent4)
	assert.Nil(err, "registered agent 4")

	applog.Debugf("subscribing agents to topics")
	assert.Nil(ebus.Subscribe(agent1, "foo", "bar"), "subscribing agent 1")
	assert.Nil(ebus.Subscribe(agent2, "foo", "baz"), "subscribing agent 2")
	assert.Nil(ebus.Subscribe(agent3, "baz", "yadda", "zong"), "subscribing agent 3")
	assert.Nil(ebus.Subscribe(agent4, "foo", "bar"), "subscribing agent 4")

	applog.Debugf("unsubscribing agents from topics")
	assert.Nil(ebus.Unsubscribe(agent3, "zong"), "usubscribing agent 3")

	applog.Debugf("unsubscribing agents from not subscribed topics")
	assert.Nil(ebus.Unsubscribe(agent3, "argle"), "usubscribing agent 3 from not subscribed topic")

	applog.Debugf("deregistering agents")
	assert.Nil(ebus.Deregister(agent4), "deregistered agent 4")
	time.Sleep(100 * time.Millisecond)
	assert.True(agent4.Stopped, "agent 4 is stopped")

	applog.Debugf("emitting events with subscribers")
	ebus.Emit(ebus.EmptyPayload, "foo")
	time.Sleep(100 * time.Millisecond)
	assert.Equal(agent1.Counters["foo"], 1, "counter foo for agent 1")
	assert.Equal(agent2.Counters["foo"], 1, "counter foo for agent 2")
	assert.Equal(agent3.Counters["foo"], 0, "counter foo for agent 3")
	assert.Equal(agent4.Counters["foo"], 0, "counter foo for agent 4")

	applog.Debugf("emitting events without subscribers")
	ebus.Emit(ebus.EmptyPayload, "iirks")
	time.Sleep(100 * time.Millisecond)
	assert.Equal(agent1.Counters["iirks"], 0, "counter iirks for agent 1")
	assert.Equal(agent2.Counters["iirks"], 0, "counter iirks for agent 2")
	assert.Equal(agent3.Counters["iirks"], 0, "counter iirks for agent 3")
	assert.Equal(agent4.Counters["iirks"], 0, "counter iirks for agent 4")

	applog.Debugf("pushing events to many subscribers")
	agents := []*ebus.TestAgent{}
	for i := 5; i < 10000; i++ {
		agent := ebus.NewTestAgent(i)
		agents = append(agents, agent)
		ebus.Register(agent)
		ebus.Subscribe(agent, "flirp")
	}
	time.Sleep(100 * time.Millisecond)
	ebus.Emit(ebus.EmptyPayload, "flirp")
	ebus.Emit(ebus.EmptyPayload, "flirp")
	ebus.Emit(ebus.EmptyPayload, "flirp")
	time.Sleep(100 * time.Millisecond)
	for i := 5; i < 10000; i++ {
		agent := agents[i-5]
		assert.Equal(agent.Counters["flirp"], 3, "counter flirp for agent")
	}
}
Пример #8
0
func StartShopAgent() {
	a, _ := ebus.Register(&ShopAgent{"Shop", nil})
	ebus.Subscribe(a, "OrderReceived")
}