blob: 14be04d1247a1aeb83639160d402057aa82f74ab [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 */
khenaidoo0927c722021-12-15 16:49:32 -050016package grpc_test
Scott Baker104b67d2019-10-29 15:56:27 -070017
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
khenaidoo0927c722021-12-15 16:49:32 -050042var logger log.CLogger
43
Scott Baker104b67d2019-10-29 15:56:27 -070044func init() {
khenaidoo0927c722021-12-15 16:49:32 -050045 // Setup this package so that it's log level can be modified at run time
46 var err error
47 logger, err = log.RegisterPackage(log.JSON, log.DebugLevel, log.Fields{})
Scott Baker104b67d2019-10-29 15:56:27 -070048 if err != nil {
49 panic(err)
50 }
51}
khenaidoo26721882021-08-11 17:42:52 -040052
53func waitUntilCondition(timeout time.Duration, verificationFunction isConditionSatisfied) error {
54 ch := make(chan int, 1)
55 done := false
56 go func() {
57 for {
58 if verificationFunction() {
59 ch <- 1
60 break
61 }
62 if done {
63 break
64 }
65 time.Sleep(retryInterval)
66 }
67 }()
68 timer := time.NewTimer(timeout)
69 defer timer.Stop()
70 select {
71 case <-ch:
72 return nil
73 case <-timer.C:
74 done = true
75 return fmt.Errorf("timeout-waiting-for-condition")
76 }
77}