blob: 9f8dd874535ca543b7c21da25d3c747f5c817d7b [file] [log] [blame]
khenaidoo68c930b2019-05-13 11:46:51 -04001/*
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 */
16package utils
17
18import (
Scott Baker807addd2019-10-24 15:16:21 -070019 "github.com/opencord/voltha-lib-go/v2/pkg/log"
khenaidoo68c930b2019-05-13 11:46:51 -040020 "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
28var (
29 timeoutError error
30 taskFailureError error
31)
32
33func 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
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050039func runSuccessfulTask(response Response, durationRange int) {
khenaidoo68c930b2019-05-13 11:46:51 -040040 time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond)
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050041 response.Done()
khenaidoo68c930b2019-05-13 11:46:51 -040042}
43
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050044func runFailureTask(response Response, durationRange int) {
khenaidoo68c930b2019-05-13 11:46:51 -040045 time.Sleep(time.Duration(rand.Intn(durationRange)) * time.Millisecond)
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050046 response.Error(taskFailureError)
khenaidoo68c930b2019-05-13 11:46:51 -040047}
48
49func 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
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050054 responses := make([]Response, numTasks)
khenaidoo68c930b2019-05-13 11:46:51 -040055 for i := 0; i < numTasks; i++ {
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050056 responses[i] = NewResponse()
khenaidoo68c930b2019-05-13 11:46:51 -040057 if numSuccessfulTaskCreated < numSuccessfulTask {
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050058 go runSuccessfulTask(responses[i], taskDurationRange)
khenaidoo68c930b2019-05-13 11:46:51 -040059 numSuccessfulTaskCreated += 1
60 continue
61 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050062 go runFailureTask(responses[i], taskDurationRange)
khenaidoo68c930b2019-05-13 11:46:51 -040063 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050064 return WaitForNilOrErrorResponses(int64(timeout), responses...)
khenaidoo68c930b2019-05-13 11:46:51 -040065}
66
67func 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
84func 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
107func 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}