Beispiel #1
0
// TestDuration tests time.Duration values.
func TestDuration(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	cfg := config.New(provider)

	cfg.Set("alpha", "0")
	cfg.Set("bravo", 5*time.Second)
	cfg.Set("charlie", "4711ms")

	// Successful gets.
	value, err := cfg.GetDuration("alpha")
	assert.Nil(err, "No error.")
	assert.Equal(value, 0*time.Second, "Right value 'alpha' returned.")
	value, err = cfg.GetDuration("bravo")
	assert.Nil(err, "No error.")
	assert.Equal(value, 5*time.Second, "Right value 'bravo' returned.")
	value, err = cfg.GetDuration("charlie")
	assert.Nil(err, "No error.")
	assert.Equal(value, 4711*time.Millisecond, "Right value 'charlie' returned.")

	// Illegal format.
	cfg.Set("illegal-format", true)
	_, err = cfg.GetDuration("illegal-format")
	assert.ErrorMatch(err, `invalid type "duration" for "true" (.*)`, "Right error returned.")

	// Non-existing key.
	_, err = cfg.GetDuration("non-existing-key")
	assert.ErrorMatch(err, `key "non-existing-key" does not exist`, "Right error returned.")

	// Non-existing key with default.
	value, err = cfg.GetDurationDefault("non-existing-key", 5*time.Hour)
	assert.Nil(err, "No error.")
	assert.Equal(value, 5*time.Hour, "Right value 'non-existing-key' returned.")
}
Beispiel #2
0
// TestTime tests time.Time values.
func TestTime(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	cfg := config.New(provider)
	now := time.Now()
	later := now.Add(12 * time.Hour)
	earlier := now.Add(-12 * time.Hour)

	cfg.Set("alpha", now)
	cfg.Set("bravo", later)

	// Successful gets.
	value, err := cfg.GetTime("alpha")
	assert.Nil(err, "No error.")
	assert.Equal(value, now, "Right value 'alpha' returned.")
	value, err = cfg.GetTime("bravo")
	assert.Nil(err, "No error.")
	assert.Equal(value, later, "Right value 'bravo' returned.")

	// Illegal format.
	cfg.Set("illegal-format", true)
	_, err = cfg.GetTime("illegal-format")
	assert.ErrorMatch(err, `invalid type "time" for "true" (.*)`, "Right error returned.")

	// Non-existing key.
	_, err = cfg.GetTime("non-existing-key")
	assert.ErrorMatch(err, `key "non-existing-key" does not exist`, "Right error returned.")

	// Non-existing key with default.
	value, err = cfg.GetTimeDefault("non-existing-key", earlier)
	assert.Nil(err, "No error.")
	assert.Equal(value, earlier, "Right value 'non-existing-key' returned.")
}
Beispiel #3
0
// TestSimple tests simple value retrieved as string.
func TestSimple(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	cfg := config.New(provider)

	cfg.Set("alpha", "quick brown fox")
	cfg.Set("bravo", true)
	cfg.Set("charlie", 4711)
	cfg.Set("delta", 47.11)

	// Successful gets.
	value, err := cfg.Get("alpha")
	assert.Nil(err, "No error.")
	assert.Equal(value, "quick brown fox", "Right value 'alpha' returned.")
	value, err = cfg.Get("bravo")
	assert.Nil(err, "No error.")
	assert.Equal(value, "true", "Right value 'bravo' returned.")
	value, err = cfg.Get("charlie")
	assert.Nil(err, "No error.")
	assert.Equal(value, "4711", "Right value 'charlie' returned.")
	value, err = cfg.Get("delta")
	assert.Nil(err, "No error.")
	assert.Equal(value, "47.11", "Right value 'delta' returned.")

	// Non-existing key.
	_, err = cfg.Get("non-existing-key")
	assert.ErrorMatch(err, `key "non-existing-key" does not exist`, "Right error returned.")

	// Non-existing key with default.
	value, err = cfg.GetDefault("non-existing-key", "default value")
	assert.Nil(err, "No error.")
	assert.Equal(value, "default value", "Right value 'non-existing-key' returned.")
}
Beispiel #4
0
// TestFloat64 tests float64 values.
func TestFloat64(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	cfg := config.New(provider)

	cfg.Set("alpha", 4711)
	cfg.Set("bravo", -47.11)
	cfg.Set("charlie", 0.0)

	// Successful gets.
	value, err := cfg.GetFloat64("alpha")
	assert.Nil(err, "No error.")
	assert.Equal(value, float64(4711), "Right value 'alpha' returned.")
	value, err = cfg.GetFloat64("bravo")
	assert.Nil(err, "No error.")
	assert.Equal(value, float64(-47.11), "Right value 'bravo' returned.")
	value, err = cfg.GetFloat64("charlie")
	assert.Nil(err, "No error.")
	assert.Equal(value, float64(0), "Right value 'charlie' returned.")

	// Illegal format.
	cfg.Set("illegal-format", true)
	_, err = cfg.GetFloat64("illegal-format")
	assert.ErrorMatch(err, `invalid type "float64" for "true" (.*)`, "Right error returned.")

	// Non-existing key.
	_, err = cfg.GetFloat64("non-existing-key")
	assert.ErrorMatch(err, `key "non-existing-key" does not exist`, "Right error returned.")

	// Non-existing key with default.
	value, err = cfg.GetFloat64Default("non-existing-key", 47.11)
	assert.Nil(err, "No error.")
	assert.Equal(value, float64(47.11), "Right value 'non-existing-key' returned.")
}
Beispiel #5
0
func InitSingle() error {
	provider := config.NewMapConfigurationProvider()
	config := config.New(provider)

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

	return Init(config)
}
Beispiel #6
0
// TestBool tests boolean values.
func TestBool(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	cfg := config.New(provider)

	cfg.Set("alpha", true)
	cfg.Set("bravo", "true")
	cfg.Set("charlie", "T")
	cfg.Set("delta", 1)
	cfg.Set("echo", false)
	cfg.Set("foxtrot", "false")
	cfg.Set("golf", "f")
	cfg.Set("hotel", 0)

	// Successful gets.
	value, err := cfg.GetBool("alpha")
	assert.Nil(err, "No error.")
	assert.True(value, "Right value 'alpha' returned.")
	value, err = cfg.GetBool("bravo")
	assert.Nil(err, "No error.")
	assert.True(value, "Right value 'bravo' returned.")
	value, err = cfg.GetBool("charlie")
	assert.Nil(err, "No error.")
	assert.True(value, "Right value 'charlie' returned.")
	value, err = cfg.GetBool("delta")
	assert.Nil(err, "No error.")
	assert.True(value, "Right value 'delta' returned.")
	value, err = cfg.GetBool("echo")
	assert.Nil(err, "No error.")
	assert.False(value, "Right value 'echo' returned.")
	value, err = cfg.GetBool("foxtrot")
	assert.Nil(err, "No error.")
	assert.False(value, "Right value 'foxtrot' returned.")
	value, err = cfg.GetBool("golf")
	assert.Nil(err, "No error.")
	assert.False(value, "Right value 'golf' returned.")
	value, err = cfg.GetBool("hotel")
	assert.Nil(err, "No error.")
	assert.False(value, "Right value 'hotel' returned.")

	// Illegal format.
	cfg.Set("illegal-format", 47.11)
	_, err = cfg.GetBool("illegal-format")
	assert.ErrorMatch(err, `invalid type "bool" for "47.11" (.*)`, "Right error returned.")

	// Non-existing key.
	_, err = cfg.GetBool("non-existing-key")
	assert.ErrorMatch(err, `key "non-existing-key" does not exist`, "Right error returned.")

	// Non-existing key with default.
	value, err = cfg.GetBoolDefault("non-existing-key", true)
	assert.Nil(err, "No error.")
	assert.True(value, "Right value 'non-existing-key' returned.")
}
Beispiel #7
0
// TestRemove tests the removing of configuration values.
func TestRemove(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	provider := config.NewMapConfigurationProvider()
	cfg := config.New(provider)

	cfg.Set("alpha", "quick brown fox")
	cfg.Set("bravo", true)
	cfg.Set("charlie", 4711)
	cfg.Set("delta", 47.11)

	cfg.Remove("bravo")
	_, err := cfg.GetBool("bravo")
	assert.ErrorMatch(err, `key "bravo" does not exist`, "Right error returned.")

	cfg.Remove("non-existing-key")
	_, err = cfg.Get("non-existing-key")
	assert.ErrorMatch(err, `key "non-existing-key" does not exist`, "Right error returned.")
}
Beispiel #8
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")
}
Beispiel #9
0
// TestStartStopSingle
func TestStartStopSingle(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")

	agent := ebus.NewTestAgent(1)
	_, err = ebus.Register(agent)
	assert.Nil(err, "agent registered")

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

	time.Sleep(100 * time.Millisecond)
	assert.True(agent.Stopped, "agent is stopped")
}
Beispiel #10
0
// runScenarioTest runs a test with the given number of orders
func runScenarioTest(assert *asserts.Asserts, param scenarioParam) {
	applog.Infof(param.String())
	monitoring.Reset()

	// Prepare test.
	done := make(chan bool)
	provider := config.NewMapConfigurationProvider()
	config := config.New(provider)

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

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

	StartShopAgent()
	StartWarehouseAgent()
	StartManufacturerAgent()
	StartDeliveryAgent()
	StartWaitAgent(done)

	// Run orders.
	for on := 0; on < param.Orders; on++ {
		order := generateOrder(on)
		err := ebus.Emit(order, "OrderReceived")
		assert.Nil(err, "order emitted")
	}

	select {
	case <-done:
		applog.Infof("order processing done")
	case <-time.After(param.Timeout):
		assert.Fail("timeout during wait for processed orders")
	}

	// Finalize test.
	err := ebus.Stop()
	assert.Nil(err, "stopped the bus")
	time.Sleep(time.Second)
	monitoring.MeasuringPointsPrintAll()
}
Beispiel #11
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")
	}
}