khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package utils |
| 17 | |
| 18 | import ( |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 19 | "math/rand" |
| 20 | "testing" |
| 21 | "time" |
| 22 | |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
| 24 | "google.golang.org/grpc/codes" |
| 25 | "google.golang.org/grpc/status" |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | timeoutError error |
| 30 | taskFailureError error |
| 31 | ) |
| 32 | |
| 33 | func init() { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 34 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 35 | taskFailureError = status.Error(codes.Internal, "test failure task") |
| 36 | } |
| 37 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 38 | func runSuccessfulTask(response Response, durationRange int) { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 39 | time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 40 | response.Done() |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 43 | func runFailureTask(response Response, durationRange int) { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 44 | time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 45 | response.Error(taskFailureError) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 46 | } |
| 47 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 48 | func runMultipleTasks(timeout time.Duration, numTasks, taskDurationRange, numSuccessfulTask, numFailuretask int) []error { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 49 | if numTasks != numSuccessfulTask+numFailuretask { |
| 50 | return []error{status.Error(codes.FailedPrecondition, "invalid-num-tasks")} |
| 51 | } |
| 52 | numSuccessfulTaskCreated := 0 |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 53 | responses := make([]Response, numTasks) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 54 | for i := 0; i < numTasks; i++ { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 55 | responses[i] = NewResponse() |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 56 | if numSuccessfulTaskCreated < numSuccessfulTask { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 57 | go runSuccessfulTask(responses[i], taskDurationRange) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 58 | numSuccessfulTaskCreated++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 59 | continue |
| 60 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 61 | go runFailureTask(responses[i], taskDurationRange) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 62 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 63 | return WaitForNilOrErrorResponses(timeout, responses...) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | func getNumSuccessFailure(inputs []error) (numSuccess, numFailure, numTimeout int) { |
| 67 | numFailure = 0 |
| 68 | numSuccess = 0 |
| 69 | numTimeout = 0 |
| 70 | for _, input := range inputs { |
| 71 | if input != nil { |
| 72 | if input.Error() == timeoutError.Error() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 73 | numTimeout++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 74 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 75 | numFailure++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 76 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 77 | numSuccess++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | func TestNoTimeouts(t *testing.T) { |
| 84 | var ( |
| 85 | totalSuccess int |
| 86 | totalFailure int |
| 87 | results []error |
| 88 | nSuccess int |
| 89 | nFailure int |
| 90 | nTimeouts int |
| 91 | ) |
| 92 | numIterations := 5 |
| 93 | numTasks := 5 |
| 94 | for i := 0; i < numIterations; i++ { |
| 95 | totalSuccess = rand.Intn(numTasks) |
| 96 | totalFailure = numTasks - totalSuccess |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 97 | results = runMultipleTasks(110*time.Millisecond, numTasks, 50, totalSuccess, totalFailure) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 98 | nSuccess, nFailure, nTimeouts = getNumSuccessFailure(results) |
| 99 | assert.Equal(t, totalFailure, nFailure) |
| 100 | assert.Equal(t, totalSuccess, nSuccess) |
| 101 | assert.Equal(t, 0, nTimeouts) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestSomeTasksTimeouts(t *testing.T) { |
| 106 | var ( |
| 107 | totalSuccess int |
| 108 | totalFailure int |
| 109 | results []error |
| 110 | nSuccess int |
| 111 | nFailure int |
| 112 | nTimeouts int |
| 113 | ) |
| 114 | numIterations := 5 |
| 115 | numTasks := 5 |
| 116 | for i := 0; i < numIterations; i++ { |
| 117 | totalSuccess = rand.Intn(numTasks) |
| 118 | totalFailure = numTasks - totalSuccess |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 119 | results = runMultipleTasks(50*time.Millisecond, numTasks, 100, totalSuccess, totalFailure) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 120 | nSuccess, nFailure, nTimeouts = getNumSuccessFailure(results) |
| 121 | assert.True(t, nFailure >= totalFailure) |
| 122 | assert.True(t, nSuccess <= totalSuccess) |
| 123 | assert.True(t, nTimeouts > 0) |
| 124 | } |
| 125 | } |