-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.go
162 lines (143 loc) · 4.19 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testutil
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"sync"
"time"
"v.io/v23/context"
"v.io/v23/discovery"
idiscovery "v.io/x/ref/lib/discovery"
)
func Advertise(ctx *context.T, p idiscovery.Plugin, adinfos ...*idiscovery.AdInfo) (func(), error) {
ctx, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
for _, adinfo := range adinfos {
wg.Add(1)
if err := p.Advertise(ctx, adinfo, wg.Done); err != nil {
cancel()
return nil, fmt.Errorf("Advertise failed: %v", err)
}
}
stop := func() {
cancel()
wg.Wait()
}
return stop, nil
}
func Scan(ctx *context.T, p idiscovery.Plugin, interfaceName string) (<-chan *idiscovery.AdInfo, func(), error) {
ctx, cancel := context.WithCancel(ctx)
scanCh := make(chan *idiscovery.AdInfo)
callback := func(ad *idiscovery.AdInfo) {
select {
case scanCh <- ad:
case <-ctx.Done():
}
}
var wg sync.WaitGroup
wg.Add(1)
if err := p.Scan(ctx, interfaceName, callback, wg.Done); err != nil {
return nil, nil, fmt.Errorf("Scan failed: %v", err)
}
stop := func() {
cancel()
wg.Wait()
}
return scanCh, stop, nil
}
func ScanAndMatch(ctx *context.T, p idiscovery.Plugin, interfaceName string, wants ...idiscovery.AdInfo) error {
const timeout = 10 * time.Second
var adinfos []idiscovery.AdInfo
for now := time.Now(); time.Since(now) < timeout; {
var err error
adinfos, err = doScan(ctx, p, interfaceName, len(wants))
if err != nil {
return err
}
if MatchFound(adinfos, wants...) {
return nil
}
}
return fmt.Errorf("Match failed; got %#v, but wanted %#v", adinfos, wants)
}
func doScan(ctx *context.T, p idiscovery.Plugin, interfaceName string, expectedAdInfos int) ([]idiscovery.AdInfo, error) {
scanCh, stop, err := Scan(ctx, p, interfaceName)
if err != nil {
return nil, err
}
defer stop()
adinfos := make([]idiscovery.AdInfo, 0, expectedAdInfos)
start := time.Now()
_, file, line, _ := runtime.Caller(2)
file = filepath.Base(file)
fmt.Printf("%v:%v: %v: doScan: waiting for %v events\n", file, line, start, expectedAdInfos)
for {
var timer <-chan time.Time
// On the first iteration, wait on scanCh with no timeout.
if len(adinfos) >= expectedAdInfos {
fmt.Printf("%v:%v: %v: doScan: bumping timer: have %v expecting %v\n", file, line, time.Since(start), len(adinfos), expectedAdInfos)
timer = time.After(5 * time.Millisecond)
}
select {
case adinfo := <-scanCh:
adinfos = append(adinfos, *adinfo)
fmt.Printf("%v:%v: %v: doScan: new: %v %v -> %v\n", file, line, time.Since(start), adinfo, len(adinfos), expectedAdInfos)
case <-timer:
fmt.Printf("%v:%v: %v: doScan: timer returning %v\n", file, line, time.Since(start), len(adinfos))
return adinfos, nil
}
}
}
func MatchFound(adinfos []idiscovery.AdInfo, wants ...idiscovery.AdInfo) bool {
return match(adinfos, false, wants...)
}
func MatchLost(adinfos []idiscovery.AdInfo, wants ...idiscovery.AdInfo) bool {
return match(adinfos, true, wants...)
}
func match(adinfos []idiscovery.AdInfo, lost bool, wants ...idiscovery.AdInfo) bool {
adinfoMap := make(map[discovery.AdId]idiscovery.AdInfo)
for _, adinfo := range adinfos {
adinfoMap[adinfo.Ad.Id] = adinfo
}
for _, want := range wants {
adinfo, ok := adinfoMap[want.Ad.Id]
if !ok {
return false
}
if lost {
if !adinfo.Lost {
return false
}
} else {
if !reflect.DeepEqual(adinfo, want) {
return false
}
}
delete(adinfoMap, want.Ad.Id)
}
return len(adinfoMap) == 0
}
func WaitUntilMatchFound(ch <-chan *idiscovery.AdInfo, want idiscovery.AdInfo) error {
return waitUntilMatches(ch, false, want)
}
func WaitUntilMatchLost(ch <-chan *idiscovery.AdInfo, want idiscovery.AdInfo) error {
return waitUntilMatches(ch, true, want)
}
func waitUntilMatches(ch <-chan *idiscovery.AdInfo, lost bool, want idiscovery.AdInfo) error {
timeout := time.After(10 * time.Second)
for {
select {
case adinfo := <-ch:
if match([]idiscovery.AdInfo{*adinfo}, lost, want) {
return nil
}
case <-timeout:
want.Lost = lost
return fmt.Errorf("Match failed; got none, but wanted %#v", want)
}
}
}