-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcvalue_test.go
More file actions
150 lines (134 loc) · 3.23 KB
/
Copy pathdcvalue_test.go
File metadata and controls
150 lines (134 loc) · 3.23 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
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
//nolint:testpackage // tests access unexported methods (InternalSet, InternalKey, etc.)
package poya
import (
"encoding/json"
"sync"
"testing"
)
func TestNewDcValue(t *testing.T) {
t.Parallel()
d := NewDcValue("hello")
if got := d.Get(); got != "hello" {
t.Errorf("Get() = %v, want %v", got, "hello")
}
}
func TestDcValueInt(t *testing.T) {
t.Parallel()
d := NewDcValue(42)
if got := d.Get(); got != 42 {
t.Errorf("Get() = %v, want %v", got, 42)
}
}
func TestDcValueInternalSet(t *testing.T) {
t.Parallel()
d := NewDcValue("initial")
d.InternalSet("updated")
if got := d.Get(); got != "updated" {
t.Errorf("Get() = %v, want %v", got, "updated")
}
}
func TestDcValueInternalKey(t *testing.T) {
t.Parallel()
d := NewDcValue("val")
d.InternalKey("mykey")
}
func TestDcValueInternalDefault(t *testing.T) {
t.Parallel()
d := NewDcValue("default_val")
if got := d.InternalDefault(); got != "default_val" {
t.Errorf("InternalDefault() = %v, want %v", got, "default_val")
}
}
func TestDcValueConcurrentGetSet(t *testing.T) {
t.Parallel()
d := NewDcValue(0)
var wg sync.WaitGroup
for i := range 100 {
wg.Add(2)
go func(v int) {
defer wg.Done()
d.InternalSet(v)
}(i)
go func() {
defer wg.Done()
_ = d.Get()
}()
}
wg.Wait()
}
func TestNewDcValueStruct(t *testing.T) {
t.Parallel()
type Config struct {
Host string
Port int
}
d := NewDcValue(Config{Host: "localhost", Port: 5432})
if d.InternalKind() != EntryKindStruct {
t.Errorf("InternalKind() = %v, want EntryKindStruct", d.InternalKind())
}
if got := d.Get(); got.Host != "localhost" || got.Port != 5432 {
t.Errorf("Get() = %+v, want {localhost 5432}", got)
}
}
func TestNewDcValueScalarKind(t *testing.T) {
t.Parallel()
d := NewDcValue(42)
if d.InternalKind() != EntryKindScalar {
t.Errorf("InternalKind() = %v, want EntryKindScalar", d.InternalKind())
}
}
func TestDcValueInternalSetJSON(t *testing.T) {
t.Parallel()
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
}
d := NewDcValue(Config{Host: "localhost", Port: 5432})
err := d.InternalSetJSON([]byte(`{"host":"remote","port":3306}`))
if err != nil {
t.Fatalf("InternalSetJSON() error: %v", err)
}
got := d.Get()
if got.Host != "remote" || got.Port != 3306 {
t.Errorf("Get() = %+v, want {remote 3306}", got)
}
}
func TestDcValueInternalSetJSONInvalid(t *testing.T) {
t.Parallel()
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
}
d := NewDcValue(Config{Host: "localhost", Port: 5432})
err := d.InternalSetJSON([]byte("invalid json"))
if err == nil {
t.Fatal("expected error for invalid JSON")
}
got := d.Get()
if got.Host != "localhost" || got.Port != 5432 {
t.Errorf("Get() should be unchanged, got %+v", got)
}
}
func TestDcValueStructConcurrentGetSet(t *testing.T) {
t.Parallel()
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
}
d := NewDcValue(Config{Host: "localhost", Port: 5432})
var wg sync.WaitGroup
for i := range 100 {
wg.Add(2)
go func(v int) {
defer wg.Done()
cfg := Config{Host: "remote", Port: v}
data, _ := json.Marshal(cfg)
_ = d.InternalSetJSON(data) //nolint:errcheck,nolintlint
}(i)
go func() {
defer wg.Done()
_ = d.Get()
}()
}
wg.Wait()
}