コード例 #1
0
ファイル: router_test.go プロジェクト: kung-foo/cellchat
// 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)
}
コード例 #2
0
ファイル: filter_test.go プロジェクト: kung-foo/cellchat
// 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")
}
コード例 #3
0
ファイル: broadcaster_test.go プロジェクト: kung-foo/cellchat
// TestBroadcasterBehavior tests the broadcast behavior.
func TestBroadcasterBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("broadcast-behavior")
	defer env.Stop()

	env.StartCell("broadcast", behaviors.NewBroadcasterBehavior())
	env.StartCell("test-1", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-2", behaviors.NewCollectorBehavior(10))
	env.Subscribe("broadcast", "test-1", "test-2")

	env.EmitNew("broadcast", "test", "a", nil)
	env.EmitNew("broadcast", "test", "b", nil)
	env.EmitNew("broadcast", "test", "c", nil)

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("test-1", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 3)

	collected, err = env.Request("test-2", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 3)
}
コード例 #4
0
ファイル: ticker_test.go プロジェクト: kung-foo/cellchat
// 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)
}
コード例 #5
0
ファイル: mapper_test.go プロジェクト: kung-foo/cellchat
// 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")
}
コード例 #6
0
ファイル: collector_test.go プロジェクト: kung-foo/cellchat
// TestCollectorBehavior tests the collector behavior.
func TestCollectorBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("collector-behavior")
	defer env.Stop()

	env.StartCell("collector", behaviors.NewCollectorBehavior(10))

	for i := 0; i < 25; i++ {
		env.EmitNew("collector", "collect", i, nil)
	}

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("collector", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 10)

	err = env.EmitNew("collector", cells.ResetTopic, nil, nil)
	assert.Nil(err)

	collected, err = env.Request("collector", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 0)
}