blob: 7a5f394b04d064e1176a302673a66543e84aae81 [file] [log] [blame]
Scott Baker104b67d2019-10-29 15:56:27 -07001/*
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 grpc
17
18import (
khenaidoo26721882021-08-11 17:42:52 -040019 "fmt"
20 "time"
21
22 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Scott Baker104b67d2019-10-29 15:56:27 -070023)
24
25const (
26 /*
khenaidoob332f9b2020-01-16 16:25:26 -050027 * This sets the GetLogLevel of the Voltha logger. It's pinned to FatalLevel here, as we
Scott Baker104b67d2019-10-29 15:56:27 -070028 * generally don't want to see logger output, even when running go test in verbose
29 * mode. Even "Error" level messages are expected to be output by some unit tests.
30 *
31 * If you are developing a unit test, and experiencing problems or wish additional
32 * debugging from Voltha, then changing this constant to log.DebugLevel may be
33 * useful.
34 */
35
khenaidoo26721882021-08-11 17:42:52 -040036 volthaTestLogLevel = log.FatalLevel
37 retryInterval = 50 * time.Millisecond
Scott Baker104b67d2019-10-29 15:56:27 -070038)
39
khenaidoo26721882021-08-11 17:42:52 -040040type isConditionSatisfied func() bool
41
Scott Baker104b67d2019-10-29 15:56:27 -070042// Unit test initialization. This init() function handles all unit tests in
43// the current directory.
44func init() {
45 // Logger must be configured or bad things happen
khenaidoo26721882021-08-11 17:42:52 -040046 _, err := log.SetDefaultLogger(log.JSON, volthaTestLogLevel, log.Fields{"instanceId": 1})
Scott Baker104b67d2019-10-29 15:56:27 -070047 if err != nil {
48 panic(err)
49 }
50}
khenaidoo26721882021-08-11 17:42:52 -040051
52func waitUntilCondition(timeout time.Duration, verificationFunction isConditionSatisfied) error {
53 ch := make(chan int, 1)
54 done := false
55 go func() {
56 for {
57 if verificationFunction() {
58 ch <- 1
59 break
60 }
61 if done {
62 break
63 }
64 time.Sleep(retryInterval)
65 }
66 }()
67 timer := time.NewTimer(timeout)
68 defer timer.Stop()
69 select {
70 case <-ch:
71 return nil
72 case <-timer.C:
73 done = true
74 return fmt.Errorf("timeout-waiting-for-condition")
75 }
76}