How to use try method of got Package

Best Got code snippet using got.try

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

officer_test.go

Source: officer_test.go Github

copy
1/*
2 * Copyright 2018-2022, CS Systemes d'Information, http://csgroup.eu
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
17package retry
18
19import (
20	"reflect"
21	"testing"
22	"time"
23
24	"github.com/stretchr/testify/require"
25)
26
27func Test_Constant(t *testing.T) {
28
29	var d time.Duration = -100
30	got := Constant(d)
31	require.EqualValues(t, reflect.TypeOf(got).String(), "*retry.Officer")
32
33	d = 100 * time.Millisecond
34	now := time.Now()
35	got = Constant(d)
36	got.Block(Try{})
37	got.Block(Try{})
38	got.Block(Try{})
39	elapsed := time.Since(now)
40	if elapsed <= 200*time.Millisecond && elapsed >= 400*time.Millisecond {
41		t.Error("Out of expected duration bounds")
42		t.Fail()
43	}
44
45}
46
47func Test_Incremental(t *testing.T) {
48
49	var d time.Duration = -100
50	got := Incremental(d)
51	require.EqualValues(t, reflect.TypeOf(got).String(), "*retry.Officer")
52
53	d = 100 * time.Millisecond
54	now := time.Now()
55	got = Incremental(d)
56	got.Block(Try{})
57	got.Block(Try{})
58	got.Block(Try{})
59	elapsed := time.Since(now)
60	if elapsed <= 200*time.Millisecond && elapsed >= 400*time.Millisecond {
61		t.Error("Out of expected duration bounds")
62		t.Fail()
63	}
64
65}
66
67func Test_Linear(t *testing.T) {
68
69	var d time.Duration = -100
70	got := Linear(d)
71	require.EqualValues(t, reflect.TypeOf(got).String(), "*retry.Officer")
72
73	d = 100 * time.Millisecond
74	now := time.Now()
75	got = Linear(d)
76	got.Block(Try{})
77	got.Block(Try{})
78	got.Block(Try{})
79	elapsed := time.Since(now)
80	if elapsed <= 30*time.Microsecond && elapsed >= 100*time.Millisecond {
81		t.Error("Out of expected duration bounds")
82		t.Fail()
83	}
84
85}
86
87func Test_Exponential(t *testing.T) {
88
89	var d time.Duration = -100
90	got := Exponential(d)
91	require.EqualValues(t, reflect.TypeOf(got).String(), "*retry.Officer")
92
93	d = 100 * time.Millisecond
94	now := time.Now()
95	got = Exponential(d)
96	got.Block(Try{})
97	got.Block(Try{})
98	got.Block(Try{})
99	elapsed := time.Since(now)
100	if elapsed <= 200*time.Millisecond && elapsed >= 350*time.Millisecond {
101		t.Error("Out of expected duration bounds")
102		t.Fail()
103	}
104
105}
106
107func Test_Fibonacci(t *testing.T) {
108	now := time.Now()
109	got := Fibonacci(100 * time.Millisecond)
110	got.Block(Try{}) // 100 +- 10
111	got.Block(Try{}) // 200 +- 10
112	got.Block(Try{}) // 300 +- 10
113	got.Block(Try{}) // 500 +- 10
114	elapsed := time.Since(now)
115	if elapsed < 1000*time.Millisecond || elapsed > 1200*time.Millisecond {
116		t.Errorf("this should have been 1100 +- 40 ms: %s", elapsed)
117		t.FailNow()
118	}
119}
120
Full Screen

seqatomic_test.go

Source: seqatomic_test.go Github

copy
1// Copyright 2018 The gVisor Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package seqatomic
16
17import (
18	"sync/atomic"
19	"testing"
20	"time"
21
22	"gvisor.dev/gvisor/pkg/sync"
23)
24
25func TestSeqAtomicLoadUncontended(t *testing.T) {
26	var seq sync.SeqCount
27	const want = 1
28	data := want
29	if got := SeqAtomicLoadInt(&seq, &data); got != want {
30		t.Errorf("SeqAtomicLoadInt: got %v, wanted %v", got, want)
31	}
32}
33
34func TestSeqAtomicLoadAfterWrite(t *testing.T) {
35	var seq sync.SeqCount
36	var data int
37	const want = 1
38	seq.BeginWrite()
39	data = want
40	seq.EndWrite()
41	if got := SeqAtomicLoadInt(&seq, &data); got != want {
42		t.Errorf("SeqAtomicLoadInt: got %v, wanted %v", got, want)
43	}
44}
45
46func TestSeqAtomicLoadDuringWrite(t *testing.T) {
47	var seq sync.SeqCount
48	var data int
49	const want = 1
50	seq.BeginWrite()
51	go func() {
52		time.Sleep(time.Second)
53		data = want
54		seq.EndWrite()
55	}()
56	if got := SeqAtomicLoadInt(&seq, &data); got != want {
57		t.Errorf("SeqAtomicLoadInt: got %v, wanted %v", got, want)
58	}
59}
60
61func TestSeqAtomicTryLoadUncontended(t *testing.T) {
62	var seq sync.SeqCount
63	const want = 1
64	data := want
65	epoch := seq.BeginRead()
66	if got, ok := SeqAtomicTryLoadInt(&seq, epoch, &data); !ok || got != want {
67		t.Errorf("SeqAtomicTryLoadInt: got (%v, %v), wanted (%v, true)", got, ok, want)
68	}
69}
70
71func TestSeqAtomicTryLoadDuringWrite(t *testing.T) {
72	var seq sync.SeqCount
73	var data int
74	epoch := seq.BeginRead()
75	seq.BeginWrite()
76	if got, ok := SeqAtomicTryLoadInt(&seq, epoch, &data); ok {
77		t.Errorf("SeqAtomicTryLoadInt: got (%v, true), wanted (_, false)", got)
78	}
79	seq.EndWrite()
80}
81
82func TestSeqAtomicTryLoadAfterWrite(t *testing.T) {
83	var seq sync.SeqCount
84	var data int
85	epoch := seq.BeginRead()
86	seq.BeginWrite()
87	seq.EndWrite()
88	if got, ok := SeqAtomicTryLoadInt(&seq, epoch, &data); ok {
89		t.Errorf("SeqAtomicTryLoadInt: got (%v, true), wanted (_, false)", got)
90	}
91}
92
93func BenchmarkSeqAtomicLoadIntUncontended(b *testing.B) {
94	var seq sync.SeqCount
95	const want = 42
96	data := want
97	b.RunParallel(func(pb *testing.PB) {
98		for pb.Next() {
99			if got := SeqAtomicLoadInt(&seq, &data); got != want {
100				b.Fatalf("SeqAtomicLoadInt: got %v, wanted %v", got, want)
101			}
102		}
103	})
104}
105
106func BenchmarkSeqAtomicTryLoadIntUncontended(b *testing.B) {
107	var seq sync.SeqCount
108	const want = 42
109	data := want
110	b.RunParallel(func(pb *testing.PB) {
111		epoch := seq.BeginRead()
112		for pb.Next() {
113			if got, ok := SeqAtomicTryLoadInt(&seq, epoch, &data); !ok || got != want {
114				b.Fatalf("SeqAtomicTryLoadInt: got (%v, %v), wanted (%v, true)", got, ok, want)
115			}
116		}
117	})
118}
119
120// For comparison:
121func BenchmarkAtomicValueLoadIntUncontended(b *testing.B) {
122	var a atomic.Value
123	const want = 42
124	a.Store(int(want))
125	b.RunParallel(func(pb *testing.PB) {
126		for pb.Next() {
127			if got := a.Load().(int); got != want {
128				b.Fatalf("atomic.Value.Load: got %v, wanted %v", got, want)
129			}
130		}
131	})
132}
133
Full Screen

parse_test.go

Source: parse_test.go Github

copy
1// Copyright 2020 Alan Tracey Wootton
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16package badjson_test
17
18import (
19	"encoding/base64"
20	"encoding/hex"
21	"fmt"
22	"reflect"
23	"strings"
24	"testing"
25
26	"github.com/awootton/knotfreeiot/badjson"
27)
28
29/** Notes. Parse string into a linked list of segments.
30
31$ starts hex array of bytes
32
33" and ' start string with escape character `\`.
34
35- and + start decimal number with -$ and +$ for hex
36
37space and : and , are separators.
38
39= starts base64 data
40
41{ and [ start a recursion.
42
43Everything else is a utf string.
44
45Code coverage is 100%.
46
47*/
48
49func ExampleChop() {
50
51	someText := `abc:def,ghi:jkl`
52
53	// parse the text
54	segment, err := badjson.Chop(someText)
55	if err != nil {
56		fmt.Println(err)
57	}
58	// traverse the result
59	for s := segment; s != nil; s = s.Next() {
60		fmt.Println(reflect.TypeOf(s))
61	}
62	// output it
63	output := badjson.ToString(segment)
64	fmt.Println(output)
65
66	someText = `"abc""def""ghi""jkl"`
67	segment, err = badjson.Chop(someText)
68	if err != nil {
69		fmt.Println(err)
70	}
71	output = badjson.ToString(segment)
72	fmt.Println(output)
73
74	// Expect: *badjson.RuneArray
75	// *badjson.RuneArray
76	// *badjson.RuneArray
77	// *badjson.RuneArray
78	// ["abc","def","ghi","jkl"]
79	// ["abc","def","ghi","jkl"]
80}
81
82func binaryTests() {
83
84	fmt.Println("abc", hex.EncodeToString([]byte("abc")))
85	fmt.Println("def", hex.EncodeToString([]byte("def")))
86	fmt.Println("ghi", hex.EncodeToString([]byte("ghi")))
87	fmt.Println("jkl", hex.EncodeToString([]byte("jkl")))
88
89	fmt.Println("abc", base64.RawURLEncoding.EncodeToString([]byte("abc")))
90	fmt.Println("def", base64.RawURLEncoding.EncodeToString([]byte("def")))
91	fmt.Println("ghi", base64.RawURLEncoding.EncodeToString([]byte("ghi")))
92	fmt.Println("jkl", base64.RawURLEncoding.EncodeToString([]byte("jkl")))
93
94}
95
96func TestExample(t *testing.T) {
97	binaryTests()
98	ExampleChop()
99}
100
101func TestParse1(t *testing.T) {
102
103	got := "a"
104	want := "b"
105
106	got = getOneString(`"ab\cd"`)
107	want = `abcd`
108	if got != want {
109		t.Errorf("got %v, want %v", got, want)
110	}
111	got = tryParseAndUnparse(`"ab\cd"`)
112	want = `["abcd"]`
113	if got != want {
114		t.Errorf("got %v, want %v", got, want)
115	}
116
117	got = getOneString(`"ab\'cd"`)
118	want = `ab'cd`
119	if got != want {
120		t.Errorf("got %v, want %v", got, want)
121	}
122	got = tryParseAndUnparse(`"ab\'cd"`)
123	want = `["ab\'cd"]`
124	if got != want {
125		t.Errorf("got %v, want %v", got, want)
126	}
127
128	got = getOneString(`"ab\"cd"`)
129	want = `ab"cd`
130	if got != want {
131		t.Errorf("got %v, want %v", got, want)
132	}
133	got = tryParseAndUnparse(`"ab\"cd"`)
134	want = `["ab\"cd"]`
135	if got != want {
136		t.Errorf("got %v, want %v", got, want)
137	}
138
139	got = getOneString(`"ab\\cd"`)
140	want = `ab\cd`
141	if got != want {
142		t.Errorf("got %v, want %v", got, want)
143	}
144	got = tryParseAndUnparse(`"ab\\cd"`)
145	want = `["ab\\cd"]`
146	if got != want {
147		t.Errorf("got %v, want %v", got, want)
148	}
149
150	got = getOneString(`    ab\"cd   `)
151	backslash := rune(92)
152	quote := rune(34)
153	if rune([]byte(got)[2]) != backslash {
154		t.Errorf("got %v, want %v", []byte(got)[2], backslash)
155	}
156	if rune([]byte(got)[3]) != quote {
157		t.Errorf("got %v, want %v", []byte(got)[2], backslash)
158	}
159	want = `ab\"cd`
160	if got != want {
161		t.Errorf("got %v, want %v", got, want)
162	}
163	got = tryParseAndUnparse(`ab\"cd`)
164	want = `["ab\\\"cd"]`
165	if got != want {
166		t.Errorf("got %v, want %v", got, want)
167	}
168
169}
170
171func xxTestParse2(t *testing.T) {
172
173	got := "a"
174	want := "b"
175
176	got = tryParseAndUnparse("+2e-2")
177	want = `[+0.02]`
178	if got != want {
179		t.Errorf("got %v, want %v", got, want)
180	}
181
182	got = tryParseAndUnparse(" +1234567 ")
183	want = `[+1234567]`
184	if got != want {
185		t.Errorf("got %v, want %v", got, want)
186	}
187
188	got = tryParseAndUnparse(" +11e2 +.0001  +2e-2")
189	want = `[+1100,+0.0001,+0.02]`
190	if got != want {
191		t.Errorf("got %v, want %v", got, want)
192	}
193}
194
195func TestParse3(t *testing.T) {
196
197	got := "a"
198	want := "b"
199
200	got = tryParseAndUnparse("{a b}}")
201	want = `[{"a":"b"},"}"]`
202	if got != want {
203		t.Errorf("got %v, want %v", got, want)
204	}
205
206	got = tryParseAndUnparse("[a b]]")
207	want = `[["a","b"],"]"]`
208	if got != want {
209		t.Errorf("got %v, want %v", got, want)
210	}
211
212	got = tryParseAndUnparse("{{{{{{{{{{{{{{{{a b}}}}}}}}}}}}}}}}")
213	want = `[{{{{{{{{{{{{{{{}:"a","b"}}}}}}}}}}}}}},"}}"]`
214	if got != want {
215		t.Errorf("got %v, want %v", got, want)
216	}
217
218	got = tryParseAndUnparse("{{{{{{{{{{{{{{{{{{a b}")
219	// note that it refuses to recurse that deep so the last '{'
220	// becomes a sibling and not a child. bad parser. bad.
221	want = `[{{{{{{{{{{{{{{{}:{},"a":"b"}}}}}}}}}}}}}}]`
222	if got != want {
223		t.Errorf("got %v, want %v", got, want)
224	}
225
226}
227
228func TestParse4(t *testing.T) {
229
230	got := "a"
231	want := "b"
232
233	// got = tryParseAndUnparse(" $1234 $45678 +$1234 -$8765")
234	// want = `["$1234","$456780",+4660,-34661]`
235	// if got != want {
236	// 	t.Errorf("got %v, want %v", got, want)
237	// }
238
239	got = tryParseAndUnparse("   aaa : bbb ")
240	want = `["aaa","bbb"]`
241	if got != want {
242		t.Errorf("got %v, want %v", got, want)
243	}
244
245	got = tryParseAndUnparse("{{ osiojdhnd : hhh44 [[[ }    ")
246	want = `[{{"osiojdhnd":"hhh44",[[["}"]]]}}]`
247	if got != want {
248		t.Errorf("got %v, want %v", got, want)
249	}
250
251	got = tryParseAndUnparse(" {a:b,c:d}  ")
252	want = `[{"a":"b","c":"d"}]`
253	if got != want {
254		t.Errorf("got %v, want %v", got, want)
255	}
256
257	got = tryParseAndUnparse(" a ")
258	want = `["a"]`
259	if got != want {
260		t.Errorf("got %v, want %v", got, want)
261	}
262
263	got = tryParseAndUnparse(" a")
264	want = `["a"]`
265	if got != want {
266		t.Errorf("got %v, want %v", got, want)
267	}
268
269	got = tryParseAndUnparse("abc ")
270	want = `["abc"]`
271	if got != want {
272		t.Errorf("got %v, want %v", got, want)
273	}
274
275	// got = tryParseAndUnparse("a +1234 MyName_var " + `"quoted string"` + " 'another' " + "=isuhe48r8dhbsvs  ")
276	// want = `["a",+1234,"MyName_var","quoted string","another","=isuhe48r8dhbsvs"]`
277	// if got != want {
278	// 	t.Errorf("got %v, want %v", got, want)
279	// }
280
281	got = tryParseAndUnparse("[      []]   ")
282	want = `[[]]`
283	if got != want {
284		t.Errorf("got %v, want %v", got, want)
285	}
286
287	// exponents and fractions are zeros. ++ is two zeros.
288	// got = tryParseAndUnparse("+1+2+3-4 ++++ ---- ++$+ --$- +1e4 +7e-4 fails!! +.00007 is_ok ")
289	// want = `[+1,+2,+3,-4,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+10000,+0.0007,"fails!!",+7e-05,"is_ok"]`
290	// if got != want {
291	// 	t.Errorf("got %v, want %v", got, want)
292	// }
293}
294
295func getOneString(input string) string {
296	segment, err := badjson.Chop(input)
297	ra, ok := segment.(*badjson.RuneArray)
298	if !ok {
299		return "this is not the string you were looking for " + err.Error()
300	}
301	return ra.GetString()
302}
303
304// check for zombies
305func TestParseZ(t *testing.T) {
306
307	got := "a"
308	want := "b"
309	var sb strings.Builder
310	for i := 0; i < 1024; i++ {
311		sb.WriteString("0123456789abcdef")
312	}
313	got = getOneString(sb.String())
314	want = sb.String()
315	if got != want {
316		t.Errorf("got %v, want %v", got, want[0:100])
317	}
318	sb.WriteString("a")
319	got = getOneString(sb.String())
320	want = `this is not the string you were looking for is longer than 16k`
321	if got != want {
322		t.Errorf("got %v, want %v", got, want)
323	}
324
325	got = tryParseAndUnparse(" ,")
326	want = `[]`
327	if got != want {
328		t.Errorf("got %v, want %v", got, want)
329	}
330
331	got = tryParseAndUnparse(" , ")
332	want = `[]`
333	if got != want {
334		t.Errorf("got %v, want %v", got, want)
335	}
336
337	got = tryParseAndUnparse(" aaa $")
338	want = `["aaa","$"]`
339	if got != want {
340		t.Errorf("got %v, want %v", got, want)
341	}
342
343	got = tryParseAndUnparse(" aaa $F")
344	want = `["aaa","$f0"]`
345	if got != want {
346		t.Errorf("got %v, want %v", got, want)
347	}
348
349	got = tryParseAndUnparse(` aaa "`)
350	want = `["aaa"]`
351	if got != want {
352		t.Errorf("got %v, want %v", got, want)
353	}
354
355	got = tryParseAndUnparse(` aaa "\`)
356	want = `["aaa",""]`
357	if got != want {
358		t.Errorf("got %v, want %v", got, want)
359	}
360
361	got = tryParseAndUnparse(` "unterminated`)
362	want = `["unterminated"]`
363	if got != want {
364		t.Errorf("got %v, want %v", got, want)
365	}
366
367	// got = tryParseAndUnparse(` +`)
368	// want = `[]`
369	// if got != want {
370	// 	t.Errorf("got %v, want %v", got, want)
371	// }
372	// got = tryParseAndUnparse(` +1`)
373	// want = `[+1]`
374	// if got != want {
375	// 	t.Errorf("got %v, want %v", got, want)
376	// }
377	// got = tryParseAndUnparse(` +1e`)
378	// want = `[+0]` // because it's a float parse error
379	// if got != want {
380	// 	t.Errorf("got %v, want %v", got, want)
381	// }
382
383	// got = tryParseAndUnparse(` +1e+`)
384	// want = `[+0]` // because it's a float parse error
385	// if got != want {
386	// 	t.Errorf("got %v, want %v", got, want)
387	// }
388
389	got = tryParseAndUnparse(` =`)
390	want = `["="]` // because empty
391	if got != want {
392		t.Errorf("got %v, want %v", got, want)
393	}
394
395	got = tryParseAndUnparse(` =ABC`)
396	want = `["=ABA"]`
397	if got != want {
398		t.Errorf("got %v, want %v", got, want)
399	}
400
401	got = tryParseAndUnparse(` $`) // ends before hex
402	want = `["$"]`
403	if got != want {
404		t.Errorf("got %v, want %v", got, want)
405	}
406	got = tryParseAndUnparse(` $smelly  `) // ends before hex
407	want = `["$","smelly"]`
408	if got != want {
409		t.Errorf("got %v, want %v", got, want)
410	}
411	got = tryParseAndUnparse(` $  `) // ends before hex
412	want = `["$"]`
413	if got != want {
414		t.Errorf("got %v, want %v", got, want)
415	}
416
417	got = tryParseAndUnparse(` =a==  `) // base64 parse error
418	want = `["="]`
419	if got != want {
420		t.Errorf("got %v, want %v", got, want)
421	}
422	got = tryParseAndUnparse(` =a=`) // base64 parse error
423	want = `["="]`
424	if got != want {
425		t.Errorf("got %v, want %v", got, want)
426	}
427	got = tryParseAndUnparse(` =aa=`)
428	want = `["=aQ"]`
429	if got != want {
430		t.Errorf("got %v, want %v", got, want)
431	}
432
433	got = tryParseAndUnparse(`{`)
434	want = `[]`
435	if got != want {
436		t.Errorf("got %v, want %v", got, want)
437	}
438
439	abase := badjson.Base{}
440	got = abase.String()
441	want = `""`
442	if got != want {
443		t.Errorf("got %v, want %v", got, want)
444	}
445
446}
447
448func tryParseAndUnparse(str string) string {
449
450	segment, err := badjson.Chop(str)
451	if err != nil {
452		return "ERROR_" + err.Error()
453	}
454
455	result := badjson.ToString(segment)
456
457	segment2, _ := badjson.Chop(result[1 : len(result)-1])
458
459	result2 := badjson.ToString(segment2)
460
461	if result != result2 {
462		return result + "!=" + result2
463	}
464	return result
465}
466
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)