github.com/blend/go-sdk@v1.20220411.3/async/future_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package async 9 10 import ( 11 "context" 12 "testing" 13 14 "github.com/blend/go-sdk/assert" 15 ) 16 17 func Test_Future_Complete(t *testing.T) { 18 its := assert.New(t) 19 20 shouldComplete := make(chan struct{}) 21 didComplete := make(chan struct{}) 22 action := func(ctx context.Context) error { 23 select { 24 case <-ctx.Done(): 25 return nil 26 case <-shouldComplete: 27 close(didComplete) 28 return nil 29 } 30 } 31 32 f := Await(context.TODO(), action) 33 34 close(shouldComplete) 35 <-didComplete 36 37 its.Nil(f.Complete()) 38 its.Nil(f.Complete(), "repeat calls should just return") 39 } 40 41 func Test_Future_Cancel(t *testing.T) { 42 its := assert.New(t) 43 44 didCancel := make(chan struct{}) 45 action := func(ctx context.Context) error { 46 <-ctx.Done() 47 close(didCancel) 48 return nil 49 } 50 51 its.Nil(Await(context.TODO(), action).Cancel()) 52 <-didCancel 53 } 54 55 func Test_Future_Cancel_repeat(t *testing.T) { 56 its := assert.New(t) 57 58 didCancel := make(chan struct{}) 59 action := func(ctx context.Context) error { 60 <-ctx.Done() 61 close(didCancel) 62 return nil 63 } 64 65 f := Await(context.TODO(), action) 66 its.Nil(f.Cancel()) 67 <-didCancel 68 69 its.Equal(ErrCannotCancel, f.Cancel()) 70 } 71 72 func Test_Future_Finished(t *testing.T) { 73 its := assert.New(t) 74 75 shouldComplete := make(chan struct{}) 76 action := func(ctx context.Context) error { 77 select { 78 case <-ctx.Done(): 79 return context.Canceled 80 case <-shouldComplete: 81 return nil 82 } 83 } 84 85 f := Await(context.TODO(), action) 86 close(shouldComplete) 87 its.Nil(<-f.Finished()) 88 }