// TestCallbackBehavior tests the callback behavior. func TestCallbackBehavior(t *testing.T) { assert := audit.NewTestingAssertion(t, true) env := cells.NewEnvironment("callback-behavior") defer env.Stop() cbdA := []string{} cbfA := func(topic string, payload cells.Payload) error { cbdA = append(cbdA, topic) return nil } cbdB := 0 cbfB := func(topic string, payload cells.Payload) error { cbdB++ return nil } sigc := audit.MakeSigChan() cbfC := func(topic string, payload cells.Payload) error { if topic == "baz" { sigc <- true } return nil } env.StartCell("callback", behaviors.NewCallbackBehavior(cbfA, cbfB, cbfC)) env.EmitNew("callback", "foo", nil, nil) env.EmitNew("callback", "bar", nil, nil) env.EmitNew("callback", "baz", nil, nil) assert.Wait(sigc, true, time.Second) assert.Equal(cbdA, []string{"foo", "bar", "baz"}) assert.Equal(cbdB, 3) }
// TestConfigurationRead tests the successful reading of a configuration. func TestConfigurationRead(t *testing.T) { assert := audit.NewTestingAssertion(t, true) env := cells.NewEnvironment("configuration-read") defer env.Stop() tempDir, filename := createConfigurationFile(assert, "{etc {foo 42}}") defer tempDir.Restore() sigc := audit.MakeSigChan() spf := func(c cells.Cell, event cells.Event) error { if event.Topic() == behaviors.ConfigurationTopic { cfg := behaviors.Configuration(event) v := cfg.ValueAsString("foo", "0815") assert.Equal(v, "42") sigc <- true } return nil } env.StartCell("configurator", behaviors.NewConfiguratorBehavior(nil)) env.StartCell("simple", behaviors.NewSimpleProcessorBehavior(spf)) env.Subscribe("configurator", "simple") pvs := cells.PayloadValues{ behaviors.ConfigurationFilenamePayload: filename, } env.EmitNew("configurator", behaviors.ReadConfigurationTopic, pvs) assert.Wait(sigc, true, 100*time.Millisecond) }
// TestConfigurationValidation tests the validation of a configuration. func TestConfigurationValidation(t *testing.T) { assert := audit.NewTestingAssertion(t, true) env := cells.NewEnvironment("configuration-validation") defer env.Stop() tempDir, filename := createConfigurationFile(assert, "{etc {foo 42}}") defer tempDir.Restore() sigc := audit.MakeSigChan() spf := func(c cells.Cell, event cells.Event) error { sigc <- true return nil } var key string cv := func(cfg etc.Etc) error { v := cfg.ValueAsString(key, "[-default-]") if v == "[-default-]" { sigc <- false } return nil } env.StartCell("configurator", behaviors.NewConfiguratorBehavior(cv)) env.StartCell("simple", behaviors.NewSimpleProcessorBehavior(spf)) env.Subscribe("configurator", "simple") // First run with success as key has the valid value "foo" pvs := cells.PayloadValues{ behaviors.ConfigurationFilenamePayload: filename, } key = "foo" env.EmitNew("configurator", behaviors.ReadConfigurationTopic, pvs) assert.Wait(sigc, true, 100*time.Millisecond) // Second run also will succeed, even with "bar" as invalid value. // See definition of validator cv above. But validationError is not // nil. key = "bar" env.EmitNew("configurator", behaviors.ReadConfigurationTopic, pvs) assert.Wait(sigc, false, 100*time.Millisecond) }
// TestAssertWait tests the wait testing. func TestAssertWait(t *testing.T) { successfulAssert := successfulAssertion(t) failingAssert := failingAssertion(t) sigc := audit.MakeSigChan() go func() { time.Sleep(50 * time.Millisecond) sigc <- true }() successfulAssert.Wait(sigc, true, 100*time.Millisecond, "should be true") go func() { time.Sleep(50 * time.Millisecond) sigc <- false }() failingAssert.Wait(sigc, true, 100*time.Millisecond, "should be false") go func() { time.Sleep(200 * time.Millisecond) sigc <- true }() failingAssert.Wait(sigc, true, 100*time.Millisecond, "should timeout") }