コード例 #1
0
ファイル: configurator_test.go プロジェクト: kung-foo/golib
// 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, "{config {foo 42}}")
	defer tempDir.Restore()

	sigc := audit.MakeSigChan()
	spf := func(ctx cells.Context, event cells.Event) error {
		if event.Topic() == behaviors.ConfigurationTopic {
			config := behaviors.Configuration(event)
			v, err := config.At("foo").Value()
			assert.Nil(err)
			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, nil)
	assert.Wait(sigc, true, 100*time.Millisecond)
}
コード例 #2
0
ファイル: configurator_test.go プロジェクト: kung-foo/golib
// 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, "{config {foo 42}}")
	defer tempDir.Restore()

	sigc := audit.MakeSigChan()
	spf := func(ctx cells.Context, event cells.Event) error {
		sigc <- true
		return nil
	}
	var key string
	cv := func(config configuration.Configuration) error {
		_, err := config.At(key).Value()
		if err != nil {
			sigc <- false
		}
		return err
	}

	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, nil)
	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, nil)
	assert.Wait(sigc, false, 100*time.Millisecond)
}