|
| 1 | +package concurrency |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/afex/hystrix-go/hystrix" |
| 11 | +) |
| 12 | + |
| 13 | +var testTask = func(ctx context.Context) TaskResult { |
| 14 | + time.Sleep(time.Millisecond * 300) |
| 15 | + return TaskResult{Result: "OK", Err: nil, CostMs: 200} |
| 16 | +} |
| 17 | +var hystrixName string |
| 18 | + |
| 19 | +func BuildHystrix(timeout int) { |
| 20 | + hystrixName = "async_hystrix" |
| 21 | + hystrix.ConfigureCommand(hystrixName, hystrix.CommandConfig{ |
| 22 | + Timeout: timeout, |
| 23 | + MaxConcurrentRequests: 2, |
| 24 | + RequestVolumeThreshold: 2, |
| 25 | + SleepWindow: timeout * 2, |
| 26 | + ErrorPercentThreshold: 5, |
| 27 | + }) |
| 28 | +} |
| 29 | +func TestHystrixHappyPath(t *testing.T) { |
| 30 | + BuildHystrix(1000) |
| 31 | + startTime := time.Now() |
| 32 | + |
| 33 | + retStub := AsynHystrix(context.TODO(), testTask, hystrixName, 1000) |
| 34 | + if time.Since(startTime).Milliseconds() > 2 { |
| 35 | + t.Error("It is not asynchronous call.") |
| 36 | + } |
| 37 | + fmt.Println("nonblocking") |
| 38 | + ret := retStub.GetResult() |
| 39 | + if ret.Err != nil { |
| 40 | + t.Error(ret.Err) |
| 41 | + } |
| 42 | + if time.Since(startTime).Microseconds() < 3000 { |
| 43 | + t.Error("It is not unexpected execution time.") |
| 44 | + } |
| 45 | + fmt.Println(ret.Result.(string), ret.Err) |
| 46 | +} |
| 47 | + |
| 48 | +func TestHystrixTimeoutPath(t *testing.T) { |
| 49 | + BuildHystrix(100) |
| 50 | + startTime := time.Now() |
| 51 | + retStub := AsynHystrix(context.TODO(), testTask, hystrixName, 100) |
| 52 | + if time.Since(startTime).Milliseconds() > 2 { |
| 53 | + t.Error("It is not asynchronous call.") |
| 54 | + } |
| 55 | + fmt.Println("nonblocking") |
| 56 | + ret := retStub.GetResult() |
| 57 | + if ret.Err == nil { |
| 58 | + t.Error("It is not unexpected execution time.") |
| 59 | + } |
| 60 | + if !errors.Is(ret.Err, ErrTimeout) { |
| 61 | + t.Error("It is unexpected error", ret.Err) |
| 62 | + } |
| 63 | + fmt.Println(ret.Result, ret.Err) |
| 64 | + |
| 65 | + // test hystrix timeout |
| 66 | + retStub = AsynHystrix(context.TODO(), testTask, hystrixName, 1000) |
| 67 | + ret = retStub.GetResult() |
| 68 | + if ret.Err == nil { |
| 69 | + t.Error("It is not unexpected execution time.") |
| 70 | + } |
| 71 | + if !errors.Is(ret.Err, hystrix.ErrTimeout) { |
| 72 | + t.Error("It is unexpected error", ret.Err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestHystrixConcurrencyPath(t *testing.T) { |
| 77 | + BuildHystrix(1000) |
| 78 | + // test hystrix max cocurrency |
| 79 | + stubs := []ResultStub{} |
| 80 | + stubs = append(stubs, AsynHystrix(context.TODO(), testTask, hystrixName, 1000)) |
| 81 | + time.Sleep(time.Millisecond * 100) |
| 82 | + stubs = append(stubs, AsynHystrix(context.TODO(), testTask, hystrixName, 1000)) |
| 83 | + time.Sleep(time.Millisecond * 100) |
| 84 | + stubs = append(stubs, AsynHystrix(context.TODO(), testTask, hystrixName, 1000)) |
| 85 | + |
| 86 | + ret := stubs[0].GetResult() |
| 87 | + if ret.Err != nil { |
| 88 | + t.Error("It is unexpected execution time.", ret.Err) |
| 89 | + } |
| 90 | + |
| 91 | + ret = stubs[1].GetResult() |
| 92 | + if ret.Err != nil { |
| 93 | + t.Error("It is unexpected execution time.", ret.Err) |
| 94 | + } |
| 95 | + |
| 96 | + ret = stubs[2].GetResult() |
| 97 | + if ret.Err == nil { |
| 98 | + t.Error("It is not unexpected execution time.") |
| 99 | + } |
| 100 | + if !errors.Is(ret.Err, hystrix.ErrMaxConcurrency) { |
| 101 | + t.Error("It is unexpected error", ret.Err) |
| 102 | + } |
| 103 | +} |
0 commit comments