(t *testing.T)
| 26 | ) |
| 27 | |
| 28 | func 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() |
nothing calls this directly
no test coverage detected