예제 #1
0
// 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)
}
예제 #2
0
// 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)
}
예제 #3
0
// TestSimpleBehavior tests the simple processor behavior.
func TestSimpleBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("simple-procesor-behavior")
	defer env.Stop()

	topics := []string{}
	var wg sync.WaitGroup
	spf := func(c cells.Cell, event cells.Event) error {
		topics = append(topics, event.Topic())
		wg.Done()
		return nil
	}
	env.StartCell("simple", behaviors.NewSimpleProcessorBehavior(spf))

	wg.Add(3)
	env.EmitNew("simple", "foo", "")
	env.EmitNew("simple", "bar", "")
	env.EmitNew("simple", "baz", "")

	wg.Wait()
	assert.Length(topics, 3)
}