Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [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 grpc |
| 17 | |
| 18 | import ( |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 19 | "fmt" |
| 20 | "time" |
| 21 | |
| 22 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | /* |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 27 | * This sets the GetLogLevel of the Voltha logger. It's pinned to FatalLevel here, as we |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 28 | * 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 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 36 | volthaTestLogLevel = log.FatalLevel |
| 37 | retryInterval = 50 * time.Millisecond |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 38 | ) |
| 39 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 40 | type isConditionSatisfied func() bool |
| 41 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 42 | // Unit test initialization. This init() function handles all unit tests in |
| 43 | // the current directory. |
| 44 | func init() { |
| 45 | // Logger must be configured or bad things happen |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 46 | _, err := log.SetDefaultLogger(log.JSON, volthaTestLogLevel, log.Fields{"instanceId": 1}) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 51 | |
| 52 | func 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 | } |