github.com/influx6/npkg@v0.8.8/nchain/futurechain_test.go (about) 1 package nchain_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 "time" 8 9 "github.com/influx6/npkg/nchain" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func ExampleFutureChain_When() { 14 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 15 return nil 16 }).When(func(_ context.Context) error { 17 return nil 18 }) 19 20 chain.Wait() 21 } 22 23 func ExampleFutureChain_Then() { 24 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 25 return nil 26 }).Then(func(_ context.Context) error { 27 return nil 28 }) 29 30 chain.Wait() 31 } 32 33 func ExampleFutureChain_Chain() { 34 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 35 return nil 36 }).Chain(func(_ context.Context) error { 37 return nil 38 }) 39 40 chain.Wait() 41 } 42 43 func TestNewFutureChain(t *testing.T) { 44 signal := make(chan struct{}, 2) 45 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 46 signal <- struct{}{} 47 return nil 48 }) 49 50 assert.NoError(t, chain.Wait()) 51 assert.Len(t, signal, 1) 52 } 53 54 func TestFutureChain_Call(t *testing.T) { 55 var firstChainTime time.Time 56 var secondChainTime time.Time 57 58 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 59 firstChainTime = time.Now() 60 return nil 61 }).Chain(func(_ context.Context) error { 62 secondChainTime = time.Now() 63 return nil 64 }) 65 66 assert.NoError(t, chain.Wait()) 67 assert.True(t, firstChainTime.Before(secondChainTime)) 68 } 69 70 func TestFutureChain_FutureCall(t *testing.T) { 71 var firstChainTime time.Time 72 var secondChainTime time.Time 73 74 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 75 firstChainTime = time.Now() 76 return nil 77 }) 78 79 chain2 := nchain.DeferredChain(context.Background(), func(_ context.Context) error { 80 secondChainTime = time.Now() 81 return nil 82 }) 83 84 chain.ChainFuture(chain2) 85 86 assert.NoError(t, chain2.Wait()) 87 assert.True(t, firstChainTime.Before(secondChainTime)) 88 } 89 90 func TestFutureChain_FutureChain(t *testing.T) { 91 firstError := errors.New("first error") 92 secondError := errors.New("first error") 93 94 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 95 return firstError 96 }) 97 98 chain2 := nchain.DeferredChain(context.Background(), func(_ context.Context) error { 99 return secondError 100 }) 101 102 chain.ChainFuture(chain2) 103 104 assert.Error(t, chain2.Wait()) 105 assert.Equal(t, chain2.Wait(), secondError) 106 } 107 108 func TestFutureChain_Chain(t *testing.T) { 109 firstError := errors.New("first error") 110 secondError := errors.New("first error") 111 112 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 113 return firstError 114 }).Chain(func(_ context.Context) error { 115 return secondError 116 }) 117 118 assert.Error(t, chain.Wait()) 119 assert.Equal(t, chain.Wait(), secondError) 120 } 121 122 func TestFutureChain_Then(t *testing.T) { 123 firstError := errors.New("first error") 124 secondError := errors.New("first error") 125 126 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 127 return firstError 128 }) 129 130 chain2 := chain.Chain(func(_ context.Context) error { 131 return secondError 132 }) 133 134 assert.Error(t, chain.Wait()) 135 assert.Equal(t, chain.Wait(), firstError) 136 assert.Equal(t, chain2.Wait(), firstError) 137 } 138 139 func TestFutureChain_ThenFuture_MultiChain(t *testing.T) { 140 firstError := errors.New("first error") 141 secondError := errors.New("first error") 142 143 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 144 return firstError 145 }) 146 147 chain2 := nchain.DeferredChain(context.Background(), func(_ context.Context) error { 148 return secondError 149 }) 150 151 chain2.Go(func(_ context.Context) error { 152 return nil 153 }) 154 155 chain2.Go(func(_ context.Context) error { 156 return nil 157 }) 158 159 chain.ThenFuture(chain2) 160 161 assert.Error(t, chain2.Wait()) 162 assert.Equal(t, chain2.Wait(), firstError) 163 } 164 165 func TestFutureChain_ThenFuture(t *testing.T) { 166 firstError := errors.New("first error") 167 secondError := errors.New("first error") 168 169 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 170 return firstError 171 }) 172 173 chain2 := nchain.DeferredChain(context.Background(), func(_ context.Context) error { 174 return secondError 175 }) 176 177 chain.ThenFuture(chain2) 178 179 assert.Error(t, chain2.Wait()) 180 assert.Equal(t, chain2.Wait(), firstError) 181 } 182 183 func TestFutureChain_WhenFuture(t *testing.T) { 184 firstError := errors.New("first error") 185 secondError := errors.New("first error") 186 187 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 188 return secondError 189 }) 190 191 signal := make(chan struct{}, 1) 192 chain2 := nchain.DeferredChain(context.Background(), func(_ context.Context) error { 193 signal <- struct{}{} 194 return firstError 195 }) 196 197 chain.WhenFuture(chain2) 198 199 assert.Error(t, chain2.Wait()) 200 assert.Equal(t, chain2.Wait(), secondError) 201 assert.Len(t, signal, 0) 202 } 203 204 func TestFutureChain_When(t *testing.T) { 205 firstError := errors.New("first error") 206 207 chain := nchain.NewFutureChain(context.Background(), func(_ context.Context) error { 208 return firstError 209 }) 210 211 signal := make(chan struct{}, 1) 212 secondChain := chain.When(func(_ context.Context) error { 213 signal <- struct{}{} 214 return nil 215 }) 216 217 assert.Error(t, secondChain.Wait()) 218 assert.Equal(t, secondChain.Wait(), firstError) 219 assert.Len(t, signal, 0) 220 }