...
1 package main
2
3 import (
4 "fmt"
5 "sync"
6 "time"
7
8 ge "golang.conradwood.net/apis/getestservice"
9 "golang.conradwood.net/go-easyops/authremote"
10 "golang.conradwood.net/go-easyops/cmdline"
11 )
12
13 var (
14 sleep_wg sync.WaitGroup
15 )
16
17 func sleepTests() {
18 cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
19 sleep_wg.Add(1)
20 go sleepTest1(time.Duration(11) * time.Second)
21 time.Sleep(time.Duration(1) * time.Second)
22
23 sleep_wg.Add(1)
24 go sleepTest1(time.Duration(20) * time.Second)
25 time.Sleep(time.Duration(1) * time.Second)
26
27 cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
28 sleep_wg.Add(1)
29 go sleepTest1(time.Duration(11) * time.Second)
30 time.Sleep(time.Duration(1) * time.Second)
31
32 sleep_wg.Add(1)
33 go sleepTest1(time.Duration(20) * time.Second)
34 time.Sleep(time.Duration(1) * time.Second)
35
36 cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
37 sleep_wg.Add(1)
38 go sleepTest2(time.Duration(20) * time.Second)
39 time.Sleep(time.Duration(1) * time.Second)
40
41 sleep_wg.Add(1)
42 go sleepTest2(time.Duration(20) * time.Second)
43 time.Sleep(time.Duration(1) * time.Second)
44
45 cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
46 sleep_wg.Add(1)
47 go sleepTest2(time.Duration(20) * time.Second)
48 time.Sleep(time.Duration(1) * time.Second)
49
50 sleep_wg.Add(1)
51 go sleepTest2(time.Duration(20) * time.Second)
52 time.Sleep(time.Duration(1) * time.Second)
53
54 cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
55 sleep_wg.Add(1)
56 go sleepTest3()
57 time.Sleep(time.Duration(1) * time.Second)
58
59 cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
60 sleep_wg.Add(1)
61 go sleepTest3()
62 time.Sleep(time.Duration(1) * time.Second)
63
64 sleep_wg.Wait()
65 }
66 func waitForSleepTests() {
67 }
68
69 func sleepTest1(dur time.Duration) {
70 t := NewTest("sleep test 1 for %0.2fs", dur.Seconds())
71 ctx := authremote.ContextWithTimeout(dur)
72 sl_seecs := dur.Seconds() - 2.0
73 _, err := ge.GetCtxTestClient().Sleep(ctx, &ge.SleepRequest{Seconds: sl_seecs})
74 t.Error(err)
75 t.Done()
76 sleep_wg.Done()
77 }
78 func sleepTest2(dur time.Duration) {
79 t := NewTest("sleep test 2 for %0.2fs", dur.Seconds())
80 sl_seecs := dur.Seconds() - 2.0
81 ctx := authremote.ContextWithTimeout(dur)
82 ctx = authremote.DerivedContextWithRouting(ctx, make(map[string]string), true)
83 if ctx == nil {
84 t.Error(fmt.Errorf("no context"))
85 sleep_wg.Done()
86 t.Done()
87 return
88 }
89 _, err := ge.GetCtxTestClient().Sleep(ctx, &ge.SleepRequest{Seconds: sl_seecs})
90 t.Error(err)
91 t.Done()
92 sleep_wg.Done()
93 }
94
95
96 func sleepTest3() {
97 sl_seecs := 20.0
98 t := NewTest("sleep test 3 for %0.2fs", sl_seecs)
99 ctxdur := time.Duration(5) * time.Second
100 ctx := authremote.ContextWithTimeout(ctxdur)
101 ctx = authremote.DerivedContextWithRouting(ctx, make(map[string]string), true)
102 if ctx == nil {
103 t.Error(fmt.Errorf("no context"))
104 sleep_wg.Done()
105 t.Done()
106 return
107 }
108 started := time.Now()
109 _, err := ge.GetCtxTestClient().Sleep(ctx, &ge.SleepRequest{Seconds: sl_seecs})
110 if err == nil {
111 t.Error(fmt.Errorf("Context with %0.2fs and sleep of %0.2fs did not time out", ctxdur.Seconds(), sl_seecs))
112 }
113 dur := time.Since(started)
114 deviation := perc_diff(dur.Seconds(), ctxdur.Seconds())
115 if deviation > 10 {
116 t.Error(fmt.Errorf("Context with %0.2fs, but timeout occured after %0.2fs (deviation %f%%)", ctxdur.Seconds(), dur.Seconds(), deviation))
117 }
118 t.Done()
119 sleep_wg.Done()
120 }
121
122 func perc_diff(a, b float64) float64 {
123 diff := a / b
124 if a < b {
125 diff = b / a
126 }
127 res := diff * 100
128 if res > 100 {
129 return res - 100
130 }
131 return 100 - res
132 }
133
View as plain text