Exemple #1
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")
}
Exemple #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
}
Exemple #3
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
}