|
| 1 | +package demux_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/floatdrop/demux" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDynamic_BasicDemuxing(t *testing.T) { |
| 12 | + type input struct { |
| 13 | + id int |
| 14 | + data string |
| 15 | + } |
| 16 | + |
| 17 | + in := make(chan input) |
| 18 | + var mu sync.Mutex |
| 19 | + result := make(map[int][]string) |
| 20 | + |
| 21 | + go demux.Dynamic(in, |
| 22 | + func(i input) int { return i.id }, |
| 23 | + func(k int, ch <-chan input) { |
| 24 | + var items []string |
| 25 | + for item := range ch { |
| 26 | + items = append(items, item.data) |
| 27 | + } |
| 28 | + mu.Lock() |
| 29 | + result[k] = items |
| 30 | + mu.Unlock() |
| 31 | + }) |
| 32 | + |
| 33 | + // Send data |
| 34 | + go func() { |
| 35 | + in <- input{id: 1, data: "a"} |
| 36 | + in <- input{id: 2, data: "b"} |
| 37 | + in <- input{id: 1, data: "c"} |
| 38 | + in <- input{id: 2, data: "d"} |
| 39 | + close(in) |
| 40 | + }() |
| 41 | + |
| 42 | + time.Sleep(100 * time.Millisecond) |
| 43 | + |
| 44 | + // Assertions |
| 45 | + if len(result) != 2 { |
| 46 | + t.Fatalf("expected 2 keys, got %d", len(result)) |
| 47 | + } |
| 48 | + if len(result[1]) != 2 || result[1][0] != "a" || result[1][1] != "c" { |
| 49 | + t.Errorf("unexpected data for key 1: %v", result[1]) |
| 50 | + } |
| 51 | + if len(result[2]) != 2 || result[2][0] != "b" || result[2][1] != "d" { |
| 52 | + t.Errorf("unexpected data for key 2: %v", result[2]) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestDynamic_SingleKey(t *testing.T) { |
| 57 | + in := make(chan int) |
| 58 | + var collected []int |
| 59 | + done := make(chan struct{}) |
| 60 | + |
| 61 | + go demux.Dynamic( |
| 62 | + in, |
| 63 | + func(i int) string { return "only" }, |
| 64 | + func(k string, ch <-chan int) { |
| 65 | + for i := range ch { |
| 66 | + collected = append(collected, i) |
| 67 | + } |
| 68 | + close(done) |
| 69 | + }) |
| 70 | + |
| 71 | + go func() { |
| 72 | + in <- 10 |
| 73 | + in <- 20 |
| 74 | + in <- 30 |
| 75 | + close(in) |
| 76 | + }() |
| 77 | + |
| 78 | + select { |
| 79 | + case <-done: |
| 80 | + case <-time.After(time.Second): |
| 81 | + t.Fatal("Timeout waiting for consumer") |
| 82 | + } |
| 83 | + |
| 84 | + expected := []int{10, 20, 30} |
| 85 | + for i, val := range expected { |
| 86 | + if collected[i] != val { |
| 87 | + t.Errorf("expected %d at index %d, got %d", val, i, collected[i]) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestDynamic_NoInput(t *testing.T) { |
| 93 | + in := make(chan string) |
| 94 | + called := false |
| 95 | + |
| 96 | + go demux.Dynamic( |
| 97 | + in, |
| 98 | + func(s string) int { return 0 }, |
| 99 | + func(k int, ch <-chan string) { |
| 100 | + called = true |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + close(in) |
| 105 | + |
| 106 | + time.Sleep(100 * time.Millisecond) |
| 107 | + if called { |
| 108 | + t.Error("consumeFunc should not be called for empty input") |
| 109 | + } |
| 110 | +} |
0 commit comments