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 | */ |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 16 | package grpc_test |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 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 | |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 42 | var logger log.CLogger |
| 43 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 44 | func init() { |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 45 | // 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 Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 48 | if err != nil { |
| 49 | panic(err) |
| 50 | } |
| 51 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 52 | |
| 53 | func 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 | } |