github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/28_rx/started/stared_test.go (about) 1 package main 2 3 import ( 4 "context" 5 "errors" 6 "github.com/reactivex/rxgo/v2" 7 "strconv" 8 "testing" 9 "time" 10 ) 11 12 func TestHello(t *testing.T) { 13 observable := rxgo.Just("Hello, World!")() 14 ch := observable.Observe() 15 item := <-ch 16 if item.Error() { 17 t.Error(item.E) 18 } 19 t.Log(item.V) 20 } 21 22 func TestForeach(t *testing.T) { 23 observable := rxgo.Just(1, 2, errors.New("unknown"), 3, 4)() 24 25 <-observable. 26 Map(func(context context.Context, i2 interface{}) (i interface{}, e error) { 27 i = strconv.Itoa(i2.(int)) + "___" 28 return 29 }, rxgo.WithCPUPool()). 30 ForEach(func(v interface{}) { 31 t.Logf("received: %v\n", v) 32 }, func(err error) { 33 t.Logf("error: %e\n", err) 34 }, func() { 35 t.Logf("observable is closed") 36 }) 37 } 38 39 type Customer struct { 40 ID int 41 Name, LastName string 42 Age int 43 TaxNumber string 44 } 45 46 func TestRealWorld(t *testing.T) { 47 /*// Create the input channel 48 ch := make(chan rxgo.Item) 49 // Data producer 50 go producer(ch) 51 52 // Create an Observable 53 observable := rxgo.FromChannel(ch)*/ 54 } 55 56 func TestHotObservables(t *testing.T) { 57 ch := make(chan rxgo.Item) 58 59 go func() { 60 for i := 0; i < 3; i++ { 61 //time.Sleep(1 * time.Second) 62 ch <- rxgo.Of(i) 63 } 64 close(ch) 65 }() 66 observable := rxgo.FromChannel(ch) 67 68 // First Observer 69 for item := range observable.Observe() { 70 t.Log(item.V, "_1") 71 } 72 73 // Second Observer 74 for item := range observable.Observe() { 75 t.Log(item.V, "_2") 76 } 77 t.Log("end") 78 } 79 80 func TestColdObservables(t *testing.T) { 81 82 observable := rxgo.Defer([]rxgo.Producer{func(_ context.Context, ch chan<- rxgo.Item) { 83 for i := 0; i < 3; i++ { 84 //time.Sleep(1 * time.Second) 85 ch <- rxgo.Of(i) 86 } 87 }, func(_ context.Context, ch chan<- rxgo.Item) { 88 for i := 3; i < 6; i++ { 89 //time.Sleep(1 * time.Second) 90 ch <- rxgo.Of(i) 91 } 92 }}) 93 // First Observer 94 for item := range observable.Observe() { 95 t.Log(item.V, "_1") 96 } 97 //time.Sleep(1 * time.Second) 98 99 // Second Observer 100 for item := range observable.Observe() { 101 t.Log(item.V, "_2") 102 } 103 t.Log("end") 104 } 105 106 func TestConnectable(t *testing.T) { 107 ch := make(chan rxgo.Item) 108 109 go func() { 110 for i := 1; i <= 3; i++ { 111 ch <- rxgo.Of(i) 112 } 113 close(ch) 114 }() 115 116 observable := rxgo.FromChannel(ch, rxgo.WithPublishStrategy()) // 发布模式,下面两个observable都可以收到 117 118 observable.Map(func(_ context.Context, i interface{}) (interface{}, error) { 119 return i.(int) + 1, nil 120 }).DoOnNext(func(i interface{}) { 121 t.Logf("First observer: %d\n", i) 122 }) 123 124 observable.Map(func(_ context.Context, i interface{}) (interface{}, error) { 125 return i.(int) * 2, nil 126 }).DoOnNext(func(i interface{}) { 127 t.Logf("Second observer: %d\n", i) 128 }) 129 // 只有 Connect observable才能发生数据 130 _, _ = observable.Connect(context.Background()) 131 //disposable() 132 133 for c := range observable.Observe() { 134 t.Logf("%T %v", c, c) 135 } 136 137 } 138 139 func TestSupplier(t *testing.T) { 140 observable := rxgo.Start([]rxgo.Supplier{func(_ context.Context) rxgo.Item { 141 return rxgo.Of(1) 142 }, func(_ context.Context) rxgo.Item { 143 return rxgo.Of(2) 144 }}) 145 for item := range observable.Observe() { 146 t.Logf("%v", item.V) 147 } 148 } 149 150 func TestBuffer(t *testing.T) { 151 observable := rxgo.Just(1, 2, 3, 4)() 152 153 observable = observable.BufferWithCount(3) 154 155 for item := range observable.Observe() { 156 t.Logf("%v", item.V) 157 } 158 } 159 func TestJoin(t *testing.T) { 160 observable := rxgo.Just( 161 map[string]int64{"tt": 1, "V": 1}, 162 map[string]int64{"tt": 4, "V": 2}, 163 map[string]int64{"tt": 7, "V": 3}, 164 )() 165 observableRight := rxgo.Just( 166 map[string]int64{"tt": 2, "V": 5}, 167 map[string]int64{"tt": 3, "V": 6}, 168 map[string]int64{"tt": 5, "V": 7}, 169 )() 170 171 observable.Join(func(ctx context.Context, l interface{}, r interface{}) (interface{}, error) { 172 return map[string]interface{}{ 173 "l": l, 174 "r": r, 175 }, nil 176 }, observableRight, func(i interface{}) time.Time { 177 return time.Unix(i.(map[string]int64)["tt"], 0) 178 }, rxgo.WithDuration(1)) 179 180 for item := range observable.Observe() { 181 t.Logf("%v", item.V) 182 } 183 time.Sleep(5 * time.Second) 184 }