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 ( |
| 19 | "github.com/opencord/voltha-go/common/log" |
| 20 | "github.com/stretchr/testify/assert" |
| 21 | "google.golang.org/grpc/codes" |
| 22 | "google.golang.org/grpc/status" |
| 23 | "math/rand" |
| 24 | "testing" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | timeoutError error |
| 30 | taskFailureError error |
| 31 | ) |
| 32 | |
| 33 | func init() { |
| 34 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 35 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 36 | taskFailureError = status.Error(codes.Internal, "test failure task") |
| 37 | } |
| 38 | |
| 39 | func runSuccessfulTask(ch chan interface{}, durationRange int) { |
| 40 | time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond) |
| 41 | ch <- nil |
| 42 | } |
| 43 | |
| 44 | func runFailureTask(ch chan interface{}, durationRange int) { |
| 45 | time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond) |
| 46 | ch <- taskFailureError |
| 47 | } |
| 48 | |
| 49 | func runMultipleTasks(timeout, numTasks, taskDurationRange, numSuccessfulTask, numFailuretask int) []error { |
| 50 | if numTasks != numSuccessfulTask+numFailuretask { |
| 51 | return []error{status.Error(codes.FailedPrecondition, "invalid-num-tasks")} |
| 52 | } |
| 53 | numSuccessfulTaskCreated := 0 |
| 54 | chnls := make([]chan interface{}, numTasks) |
| 55 | for i := 0; i < numTasks; i++ { |
| 56 | chnls[i] = make(chan interface{}) |
| 57 | if numSuccessfulTaskCreated < numSuccessfulTask { |
| 58 | go runSuccessfulTask(chnls[i], taskDurationRange) |
| 59 | numSuccessfulTaskCreated += 1 |
| 60 | continue |
| 61 | } |
| 62 | go runFailureTask(chnls[i], taskDurationRange) |
| 63 | } |
| 64 | return WaitForNilOrErrorResponses(int64(timeout), chnls...) |
| 65 | } |
| 66 | |
| 67 | func getNumSuccessFailure(inputs []error) (numSuccess, numFailure, numTimeout int) { |
| 68 | numFailure = 0 |
| 69 | numSuccess = 0 |
| 70 | numTimeout = 0 |
| 71 | for _, input := range inputs { |
| 72 | if input != nil { |
| 73 | if input.Error() == timeoutError.Error() { |
| 74 | numTimeout += 1 |
| 75 | } |
| 76 | numFailure += 1 |
| 77 | } else { |
| 78 | numSuccess += 1 |
| 79 | } |
| 80 | } |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | func TestNoTimeouts(t *testing.T) { |
| 85 | var ( |
| 86 | totalSuccess int |
| 87 | totalFailure int |
| 88 | results []error |
| 89 | nSuccess int |
| 90 | nFailure int |
| 91 | nTimeouts int |
| 92 | ) |
| 93 | numIterations := 5 |
| 94 | numTasks := 5 |
| 95 | for i := 0; i < numIterations; i++ { |
| 96 | totalSuccess = rand.Intn(numTasks) |
| 97 | totalFailure = numTasks - totalSuccess |
| 98 | results = runMultipleTasks(110, numTasks, 100, totalSuccess, totalFailure) |
| 99 | nSuccess, nFailure, nTimeouts = getNumSuccessFailure(results) |
| 100 | assert.Equal(t, totalFailure, nFailure) |
| 101 | assert.Equal(t, totalSuccess, nSuccess) |
| 102 | assert.Equal(t, 0, nTimeouts) |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestSomeTasksTimeouts(t *testing.T) { |
| 108 | var ( |
| 109 | totalSuccess int |
| 110 | totalFailure int |
| 111 | results []error |
| 112 | nSuccess int |
| 113 | nFailure int |
| 114 | nTimeouts int |
| 115 | ) |
| 116 | numIterations := 5 |
| 117 | numTasks := 5 |
| 118 | for i := 0; i < numIterations; i++ { |
| 119 | totalSuccess = rand.Intn(numTasks) |
| 120 | totalFailure = numTasks - totalSuccess |
| 121 | results = runMultipleTasks(50, numTasks, 100, totalSuccess, totalFailure) |
| 122 | nSuccess, nFailure, nTimeouts = getNumSuccessFailure(results) |
| 123 | assert.True(t, nFailure >= totalFailure) |
| 124 | assert.True(t, nSuccess <= totalSuccess) |
| 125 | assert.True(t, nTimeouts > 0) |
| 126 | } |
| 127 | } |