MCPcopy Create free account
hub / github.com/FunctionStream/function-stream / TestMemoryTube

Function TestMemoryTube

fs/contube/memory_test.go:28–87  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestMemoryTube(t *testing.T) {
29
30 ctx, cancel := context.WithCancel(context.Background())
31 tubeFactory := NewMemoryQueueFactory(ctx)
32 memoryQueueFactory := tubeFactory.(*MemoryQueueFactory)
33
34 var wg sync.WaitGroup
35 var events []Record
36
37 topics := []string{"topic1", "topic2", "topic3"}
38 source, err := memoryQueueFactory.NewSourceTube(ctx, (&SourceQueueConfig{Topics: topics,
39 SubName: "consume-" + strconv.Itoa(rand.Int())}).ToConfigMap())
40 if err != nil {
41 t.Fatal(err)
42 }
43
44 for i, v := range topics {
45 wg.Add(1)
46 sink, err := memoryQueueFactory.NewSinkTube(ctx, (&SinkQueueConfig{Topic: v}).ToConfigMap())
47 if err != nil {
48 t.Fatal(err)
49 }
50 go func(i int) {
51 defer wg.Done()
52 defer close(sink)
53 sink <- NewRecordImpl([]byte{byte(i + 1)}, func() {})
54 }(i)
55 }
56
57 wg.Add(1)
58 go func() {
59 defer wg.Done()
60 for {
61 select {
62 case event := <-source:
63 events = append(events, event)
64 if len(events) == len(topics) {
65 return
66 }
67 default:
68 continue
69 }
70 }
71 }()
72
73 wg.Wait()
74 cancel()
75
76 // Give enough time to ensure that the goroutine execution within NewSource Tube and NewSinkTube is complete and
77 // the released queue is successful.
78 time.Sleep(100 * time.Millisecond)
79
80 // assert the memoryQueueFactory.queues is empty.
81 memoryQueueFactory.mu.Lock()
82 if len(memoryQueueFactory.queues) != 0 {
83 t.Fatal("MemoryQueueFactory.queues is not empty")
84 }
85 memoryQueueFactory.mu.Unlock()

Callers

nothing calls this directly

Calls 5

NewMemoryQueueFactoryFunction · 0.85
NewRecordImplFunction · 0.85
NewSourceTubeMethod · 0.65
NewSinkTubeMethod · 0.65
ToConfigMapMethod · 0.45

Tested by

no test coverage detected