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 | |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 24 | "github.com/stretchr/testify/assert" |
| 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | timeoutError error |
| 31 | taskFailureError error |
| 32 | ) |
| 33 | |
| 34 | func init() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 35 | _, err := log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 36 | if err != nil { |
| 37 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 38 | } |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 39 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 40 | taskFailureError = status.Error(codes.Internal, "test failure task") |
| 41 | } |
| 42 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 43 | func runSuccessfulTask(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.Done() |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 48 | func runFailureTask(response Response, durationRange int) { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 49 | time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 50 | response.Error(taskFailureError) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | func runMultipleTasks(timeout, numTasks, taskDurationRange, numSuccessfulTask, numFailuretask int) []error { |
| 54 | if numTasks != numSuccessfulTask+numFailuretask { |
| 55 | return []error{status.Error(codes.FailedPrecondition, "invalid-num-tasks")} |
| 56 | } |
| 57 | numSuccessfulTaskCreated := 0 |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 58 | responses := make([]Response, numTasks) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 59 | for i := 0; i < numTasks; i++ { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 60 | responses[i] = NewResponse() |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 61 | if numSuccessfulTaskCreated < numSuccessfulTask { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 62 | go runSuccessfulTask(responses[i], taskDurationRange) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 63 | numSuccessfulTaskCreated++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 64 | continue |
| 65 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 66 | go runFailureTask(responses[i], taskDurationRange) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 67 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 68 | return WaitForNilOrErrorResponses(int64(timeout), responses...) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func getNumSuccessFailure(inputs []error) (numSuccess, numFailure, numTimeout int) { |
| 72 | numFailure = 0 |
| 73 | numSuccess = 0 |
| 74 | numTimeout = 0 |
| 75 | for _, input := range inputs { |
| 76 | if input != nil { |
| 77 | if input.Error() == timeoutError.Error() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 78 | numTimeout++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 79 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 80 | numFailure++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 81 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 82 | numSuccess++ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | func TestNoTimeouts(t *testing.T) { |
| 89 | var ( |
| 90 | totalSuccess int |
| 91 | totalFailure int |
| 92 | results []error |
| 93 | nSuccess int |
| 94 | nFailure int |
| 95 | nTimeouts int |
| 96 | ) |
| 97 | numIterations := 5 |
| 98 | numTasks := 5 |
| 99 | for i := 0; i < numIterations; i++ { |
| 100 | totalSuccess = rand.Intn(numTasks) |
| 101 | totalFailure = numTasks - totalSuccess |
| 102 | results = runMultipleTasks(110, numTasks, 100, totalSuccess, totalFailure) |
| 103 | nSuccess, nFailure, nTimeouts = getNumSuccessFailure(results) |
| 104 | assert.Equal(t, totalFailure, nFailure) |
| 105 | assert.Equal(t, totalSuccess, nSuccess) |
| 106 | assert.Equal(t, 0, nTimeouts) |
| 107 | |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestSomeTasksTimeouts(t *testing.T) { |
| 112 | var ( |
| 113 | totalSuccess int |
| 114 | totalFailure int |
| 115 | results []error |
| 116 | nSuccess int |
| 117 | nFailure int |
| 118 | nTimeouts int |
| 119 | ) |
| 120 | numIterations := 5 |
| 121 | numTasks := 5 |
| 122 | for i := 0; i < numIterations; i++ { |
| 123 | totalSuccess = rand.Intn(numTasks) |
| 124 | totalFailure = numTasks - totalSuccess |
| 125 | results = runMultipleTasks(50, numTasks, 100, totalSuccess, totalFailure) |
| 126 | nSuccess, nFailure, nTimeouts = getNumSuccessFailure(results) |
| 127 | assert.True(t, nFailure >= totalFailure) |
| 128 | assert.True(t, nSuccess <= totalSuccess) |
| 129 | assert.True(t, nTimeouts > 0) |
| 130 | } |
| 131 | } |