当前位置: 首页>>代码示例>>Golang>>正文


Golang cells.NewEnvironment函数代码示例

本文整理汇总了Golang中github.com/tideland/golib/cells.NewEnvironment函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEnvironment函数的具体用法?Golang NewEnvironment怎么用?Golang NewEnvironment使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewEnvironment函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestEnvironment

// TestEnvironment tests general environment methods.
func TestEnvironment(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	envOne := cells.NewEnvironment("part", 1, "of", "env", "ONE")
	defer envOne.Stop()

	id := envOne.ID()
	assert.Equal(id, "part:1:of:env:one")

	envTwo := cells.NewEnvironment("environment TWO")
	defer envTwo.Stop()

	id = envTwo.ID()
	assert.Equal(id, "environment-two")
}
开发者ID:reborn2005,项目名称:golib,代码行数:16,代码来源:cells_test.go

示例2: TestFilterBehavior

// TestFilterBehavior tests the filter behavior.
func TestFilterBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("filter-behavior")
	defer env.Stop()

	ff := func(id string, event cells.Event) bool {
		dp, ok := event.Payload().Get(cells.DefaultPayload)
		if !ok {
			return false
		}
		payload := dp.(string)
		return event.Topic() == payload
	}
	env.StartCell("filter", behaviors.NewFilterBehavior(ff))
	env.StartCell("collector", behaviors.NewCollectorBehavior(10))
	env.Subscribe("filter", "collector")

	env.EmitNew("filter", "a", "a", nil)
	env.EmitNew("filter", "a", "b", nil)
	env.EmitNew("filter", "b", "b", nil)

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("collector", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 2, "two collected events")
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:28,代码来源:filter_test.go

示例3: TestCallbackBehavior

// 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)
}
开发者ID:kung-foo,项目名称:golib,代码行数:34,代码来源:callback_test.go

示例4: TestRouterBehavior

// TestRouterBehavior tests the router behavior.
func TestRouterBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("router-behavior")
	defer env.Stop()

	rf := func(emitterID, subscriberID string, event cells.Event) (bool, error) {
		ok := strings.Contains(event.Topic(), subscriberID)
		return ok, nil
	}
	env.StartCell("router", behaviors.NewRouterBehavior(rf))
	env.StartCell("test-1", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-2", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-3", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-4", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-5", behaviors.NewCollectorBehavior(10))
	env.Subscribe("router", "test-1", "test-2", "test-3", "test-4", "test-5")

	env.EmitNew("router", "test-1:test-2", "a", nil)
	env.EmitNew("router", "test-1:test-2:test-3", "b", nil)
	env.EmitNew("router", "test-3:test-4:test-5", "c", nil)

	time.Sleep(100 * time.Millisecond)

	test := func(id string, length int) {
		collected, err := env.Request(id, cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
		assert.Nil(err)
		assert.Length(collected, length)
	}

	test("test-1", 2)
	test("test-2", 2)
	test("test-3", 2)
	test("test-4", 1)
	test("test-5", 1)
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:36,代码来源:router_test.go

示例5: TestEnvironmentScenario

// TestEnvironmentScenario tests creating and using the
// environment in a simple way.
func TestEnvironmentScenario(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("scenario")
	defer env.Stop()

	err := env.StartCell("foo", newCollectBehavior())
	assert.Nil(err)
	err = env.StartCell("bar", newCollectBehavior())
	assert.Nil(err)
	err = env.StartCell("collector", newCollectBehavior())
	assert.Nil(err)

	err = env.Subscribe("foo", "bar")
	assert.Nil(err)
	err = env.Subscribe("bar", "collector")
	assert.Nil(err)

	err = env.EmitNew("foo", "lorem", 4711, nil)
	assert.Nil(err)
	err = env.EmitNew("foo", "ipsum", 1234, nil)
	assert.Nil(err)
	response, err := env.Request("foo", cells.PingTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Equal(response, cells.PongResponse)

	time.Sleep(200 * time.Millisecond)

	collected, err := env.Request("collector", cells.ProcessedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 2, "two collected events")
	assert.Contents(`<topic: "lorem" / payload: <"default": 4711>>`, collected)
	assert.Contents(`<topic: "ipsum" / payload: <"default": 1234>>`, collected)
}
开发者ID:reborn2005,项目名称:golib,代码行数:35,代码来源:cells_test.go

示例6: TestConfigurationRead

// 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)
}
开发者ID:kung-foo,项目名称:golib,代码行数:31,代码来源:configurator_test.go

示例7: TestEnvironmentSubscribersDo

// TestEnvironmentSubscribersDo tests the iteration over
// the subscribers.
func TestEnvironmentSubscribersDo(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	env := cells.NewEnvironment("subscribers-do")
	defer env.Stop()

	err := env.StartCell("foo", newCollectBehavior())
	assert.Nil(err)
	err = env.StartCell("bar", newCollectBehavior())
	assert.Nil(err)
	err = env.StartCell("baz", newCollectBehavior())
	assert.Nil(err)

	err = env.Subscribe("foo", "bar", "baz")
	assert.Nil(err)
	err = env.EmitNew("foo", iterateTopic, nil, nil)
	assert.Nil(err)

	time.Sleep(200 * time.Millisecond)

	collected, err := env.Request("bar", cells.ProcessedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 1)
	assert.Contents(`<topic: "love" / payload: <"default": foo loves bar>>`, collected)
	collected, err = env.Request("baz", cells.ProcessedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 1)
	assert.Contents(`<topic: "love" / payload: <"default": foo loves baz>>`, collected)
}
开发者ID:reborn2005,项目名称:golib,代码行数:31,代码来源:cells_test.go

示例8: BenchmarkSmpleEmitNullMonitoring

// BenchmarkSmpleEmitNullMonitoring is a simple emitting to one cell
// with the null monitor.
func BenchmarkSmpleEmitNullMonitoring(b *testing.B) {
	env := cells.NewEnvironment("simple-emit")
	defer env.Stop()

	env.StartCell("collector", newCollectBehavior())

	event, _ := cells.NewEvent("foo", "bar", nil)

	for i := 0; i < b.N; i++ {
		env.Emit("collector", event)
	}
}
开发者ID:kung-foo,项目名称:golib,代码行数:14,代码来源:cells_test.go

示例9: BenchmarkSmpleEmitStandardMonitoring

// BenchmarkSmpleEmitStandardMonitoring is a simple emitting to one cell
// with the standard monitor.
func BenchmarkSmpleEmitStandardMonitoring(b *testing.B) {
	monitoring.SetBackend(monitoring.NewStandardBackend())
	env := cells.NewEnvironment("simple-emit-standard")
	defer env.Stop()

	env.StartCell("null", &nullBehavior{})

	event, _ := cells.NewEvent("foo", "bar", nil)

	for i := 0; i < b.N; i++ {
		env.Emit("null", event)
	}
}
开发者ID:reborn2005,项目名称:golib,代码行数:15,代码来源:cells_test.go

示例10: TestTickerBehavior

// TestTickerBehavior tests the ticker behavior.
func TestTickerBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("ticker-behavior")
	defer env.Stop()

	env.StartCell("ticker", behaviors.NewTickerBehavior(50*time.Millisecond))
	env.StartCell("test", behaviors.NewCollectorBehavior(10))
	env.Subscribe("ticker", "test")

	time.Sleep(125 * time.Millisecond)

	collected, err := env.Request("test", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 2)
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:16,代码来源:ticker_test.go

示例11: main

func main() {
	golib_logger.SetLevel(golib_logger.LevelInfo)
	log.Print("start")

	rootEnvironment = cells.NewEnvironment()
	defer rootEnvironment.Stop()

	addBuilding(rootEnvironment, "school")
	addRoom(rootEnvironment, "school", "cafeteria")

	startServer(rootEnvironment)
	/*
		addRoom(rootEnvironment, "school", "playground")

		time.Sleep(time.Millisecond * 100)

		addLogUser(rootEnvironment, "school", "cafeteria")

		addUser(rootEnvironment, "school", "cafeteria", "willy")
		addUser(rootEnvironment, "school", "cafeteria", "bart")
		addUser(rootEnvironment, "school", "cafeteria", "lisa")

		time.Sleep(time.Millisecond * 100)

		bart := makeUserID("bart")

		rootEnvironment.EmitNew(bart, SAY, cells.PayloadValues{
			"room":    makeRoomID("cafeteria", "school"),
			"message": "Don't have a cow, Man!",
		}, nil)

		time.Sleep(time.Millisecond * 100)

		rootEnvironment.EmitNew(bart, SAY, cells.PayloadValues{
			"room":    makeRoomID("cafeteria", "school"),
			"message": "I'm Bart Simpson, who the hell are you?",
		}, nil)

		time.Sleep(time.Millisecond * 100)

		rootEnvironment.EmitNew(bart, SAY, cells.PayloadValues{
			"room":    makeRoomID("cafeteria", "school"),
			"message": "what teh hell ?",
		}, nil)

		time.Sleep(time.Second * 5)
	*/
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:48,代码来源:main.go

示例12: TestEnvironmentSubscribeStop

// TestEnvironmentSubscribeStop subscribing and stopping
func TestEnvironmentSubscribeStop(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	env := cells.NewEnvironment("subscribe-unsubscribe-stop")
	defer env.Stop()

	assert.Nil(env.StartCell("foo", newCollectBehavior()))
	assert.Nil(env.StartCell("bar", newCollectBehavior()))
	assert.Nil(env.StartCell("baz", newCollectBehavior()))

	assert.Nil(env.Subscribe("foo", "bar", "baz"))
	assert.Nil(env.Subscribe("bar", "foo", "baz"))

	assert.Nil(env.StopCell("bar"))
	assert.Nil(env.StopCell("foo"))
	assert.Nil(env.StopCell("baz"))
}
开发者ID:reborn2005,项目名称:golib,代码行数:18,代码来源:cells_test.go

示例13: TestMapperBehavior

// TestMapperBehavior tests the mapping of events.
func TestMapperBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	assertPayload := func(collected interface{}, index int, value string) {
		eventData, ok := collected.([]behaviors.EventData)
		assert.True(ok)
		payload, ok := eventData[index].Payload.(cells.Payload)
		assert.True(ok)
		upperText, ok := payload.Get("upper-text")
		assert.True(ok)
		assert.Equal(upperText, value)
	}
	env := cells.NewEnvironment("mapper-behavior")
	defer env.Stop()

	mf := func(id string, event cells.Event) (cells.Event, error) {
		text, ok := event.Payload().Get(cells.DefaultPayload)
		if !ok {
			return event, nil
		}
		pv := cells.PayloadValues{
			"upper-text": strings.ToUpper(text.(string)),
		}
		payload := event.Payload().Apply(pv)
		return cells.NewEvent(event.Topic(), payload, event.Scene())
	}

	env.StartCell("mapper", behaviors.NewMapperBehavior(mf))
	env.StartCell("collector", behaviors.NewCollectorBehavior(10))
	env.Subscribe("mapper", "collector")

	env.EmitNew("mapper", "a", "abc", nil)
	env.EmitNew("mapper", "b", "def", nil)
	env.EmitNew("mapper", "c", "ghi", nil)

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("collector", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 3, "three mapped events")
	assertPayload(collected, 0, "ABC")
	assertPayload(collected, 1, "DEF")
	assertPayload(collected, 2, "GHI")
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:44,代码来源:mapper_test.go

示例14: TestBehaviorEmitTimeoutError

// TestBehaviorEmitTimeoutError tests the timeout error handling
// when one or more emit need too much time.
func TestBehaviorEmitTimeoutError(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	env := cells.NewEnvironment("emit-timeout-error")
	defer env.Stop()

	err := env.StartCell("emitter", newEmitBehavior())
	assert.Nil(err)
	err = env.StartCell("sleeper", newSleepBehavior())
	assert.Nil(err)
	err = env.Subscribe("emitter", "sleeper")
	assert.Nil(err)

	// Emit more events than queue can take while the subscriber works.
	for i := 0; i < 25; i++ {
		env.EmitNew("emitter", emitTopic, i, nil)
	}

	time.Sleep(2 * time.Second)
}
开发者ID:reborn2005,项目名称:golib,代码行数:22,代码来源:cells_test.go

示例15: TestBehaviorEventBufferSize

// TestBehaviorEventBufferSize tests the setting of
// the event buffer size.
func TestBehaviorEventBufferSize(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	env := cells.NewEnvironment("event-buffer")
	defer env.Stop()

	err := env.StartCell("negative", newEventBufferBehavior(-8))
	assert.Nil(err)
	ci := cells.InspectCell(env, "negative")
	assert.Equal(ci.EventBufferSize(), cells.MinEventBufferSize)

	err = env.StartCell("low", newEventBufferBehavior(1))
	assert.Nil(err)
	ci = cells.InspectCell(env, "low")
	assert.Equal(ci.EventBufferSize(), cells.MinEventBufferSize)

	err = env.StartCell("high", newEventBufferBehavior(2*cells.MinEventBufferSize))
	assert.Nil(err)
	ci = cells.InspectCell(env, "high")
	assert.Equal(ci.EventBufferSize(), 2*cells.MinEventBufferSize)
}
开发者ID:reborn2005,项目名称:golib,代码行数:23,代码来源:cells_test.go


注:本文中的github.com/tideland/golib/cells.NewEnvironment函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。