-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
120 lines (111 loc) · 2.86 KB
/
utils_test.go
File metadata and controls
120 lines (111 loc) · 2.86 KB
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
// Copyright (C) 2025, Lux Partners Limited All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"testing"
"time"
"github.com/luxfi/constants"
"github.com/stretchr/testify/require"
)
func TestUnique(t *testing.T) {
// empty
input := []int{}
expected := []int{}
result := Unique(input)
require.Equal(t, expected, result)
// ordered input
input = []int{1, 2, 2, 3, 4, 4, 5}
expected = []int{1, 2, 3, 4, 5}
result = Unique(input)
require.Equal(t, expected, result)
// unordered input
input = []int{5, 2, 1, 3, 4, 2, 4}
expected = []int{5, 2, 1, 3, 4}
result = Unique(input)
require.Equal(t, expected, result)
}
func TestBelongs(t *testing.T) {
// empty
input := []string{}
require.False(t, Belongs(input, "orange"))
// non empty
input = []string{"apple", "banana", "cherry"}
require.True(t, Belongs(input, "banana"))
require.False(t, Belongs(input, "orange"))
}
func TestUint32Sort(t *testing.T) {
// empty
input := []uint32{}
expected := []uint32{}
Uint32Sort(input)
require.Equal(t, expected, input)
// non empty
input = []uint32{5, 3, 4, 1, 2}
expected = []uint32{1, 2, 3, 4, 5}
Uint32Sort(input)
require.Equal(t, expected, input)
}
func TestGetAPIContext(t *testing.T) {
ctx, cancel := GetAPIContext()
deadline, hasDeadline := ctx.Deadline()
timeout := time.Until(deadline)
select {
case <-ctx.Done():
// Context should not be done immediately
t.Fatal("context should not be done immediately")
default:
// Context is still active
}
require.Equal(t, true, hasDeadline)
require.Equal(t, true, timeout-constants.APIRequestTimeout < time.Millisecond)
cancel()
select {
case <-ctx.Done():
// Context should be done immediately
default:
// Context is still active
t.Fatal("context should be done immediately")
}
}
func TestGetAPILargeContext(t *testing.T) {
ctx, cancel := GetAPILargeContext()
deadline, hasDeadline := ctx.Deadline()
timeout := time.Until(deadline)
select {
case <-ctx.Done():
// Context should not be done immediately
t.Fatal("context should not be done immediately")
default:
// Context is still active
}
require.Equal(t, true, hasDeadline)
require.Equal(t, true, timeout-constants.APIRequestLargeTimeout < time.Millisecond)
cancel()
select {
case <-ctx.Done():
// Context should be done immediately
default:
// Context is still active
t.Fatal("context should be done immediately")
}
}
func TestGetTimedContext(t *testing.T) {
timeout := 50 * time.Millisecond
ctx, cancel := GetTimedContext(timeout)
defer cancel()
select {
case <-ctx.Done():
// Context should not be done immediately
t.Fatal("context should not be done immediately")
default:
// Context is still active
}
time.Sleep(timeout)
time.Sleep(1 * time.Millisecond)
select {
case <-ctx.Done():
// Context should be done after timeout
default:
t.Fatal("context should be done after timeout")
}
}