-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
250 lines (238 loc) · 6.34 KB
/
store_test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// store.go - Storage Handler test
//
// ॐ भूर्भुवः स्वः
// तत्स॑वि॒तुर्वरे॑ण्यं॒
// भर्गो॑ दे॒वस्य॑ धीमहि।
// धियो॒ यो नः॑ प्रचो॒दया॑त्॥
//
//
// बोसजी के द्वारा रचित गो-मिल तन्त्राक्ष्
// ============================
//
// यह गो-क्रमादेश आधारित एम.क्यू.टी.टी अधिलेख में प्रचालेखन का तन्त्राक्ष् है।
//
// एक रचनात्मक भारतीय उत्पाद।
//
// go-mli - Boseji's Golang MQTT Logging command line
//
// Easy to use Golang based MQTT Command line logger.
//
// Sources
// -------
// https://github.com/boseji/go-mli
//
// License
// -------
//
// SPDX: GPL-3.0-or-later
//
// go-mli - Boseji's Golang MQTT Logging command line
// Copyright (C) 2024 by Abhijit Bose (aka. Boseji)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Storage Handler test
package main
import (
"bytes"
"context"
"encoding/csv"
"os"
"strings"
"sync"
"testing"
"time"
)
const (
TEST_FILE = "test.csv"
)
func Test_storeGoroutine(t *testing.T) {
tests := []struct {
name string
fn func(t *testing.T, c chan string)
}{
{
name: "Positive test Sending message",
fn: func(t *testing.T, c chan string) {
// Create a Writable Buffer for String with CSV Format
b := bytes.NewBufferString("")
w := csv.NewWriter(b)
// Create the Record
// - Special Time format to help with automatic time recognition
// under the LibreOffice Calc for time stamp in 'CSV' format.
w.Write([]string{time.Now().Format("2006-01-02T15:04:05" /*time.RFC3339*/),
"Test1", "Test2"})
w.Flush() // For ce Write to String Buffer
// Get back the String from CSV
s := b.String()
// Send it
c <- s
time.Sleep(100 * time.Millisecond)
content, err := os.ReadFile(TEST_FILE)
if err != nil {
t.Fatal("Failed to read the generated log file")
}
if !bytes.Equal(content, []byte(STORE_HEADER+"\n"+s)) {
t.Fatalf("Expected: \n%q\n Got:\n %q", STORE_HEADER+s,
string(content))
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
c := make(chan string, 2)
wg.Add(1)
os.Remove(TEST_FILE)
go storeGoroutine(c, ctx, &wg, TEST_FILE)
time.Sleep(100 * time.Millisecond)
tt.fn(t, c)
time.Sleep(100 * time.Millisecond)
cancel()
close(c)
wg.Wait()
os.Remove(TEST_FILE)
})
}
}
func Test_recordGoroutine(t *testing.T) {
tests := []struct {
name string
fnBefore func(t *testing.T, c chan string)
msg string
fnAfter func(t *testing.T, c chan string)
}{
{
name: "Negative test with a filled channel",
fnBefore: func(t *testing.T, c chan string) {
c <- "Test 1"
c <- "Test 2"
},
msg: "Test3",
fnAfter: func(t *testing.T, c chan string) {},
},
{
name: "Test the Normal operation",
fnBefore: func(t *testing.T, c chan string) {},
msg: "Test1",
fnAfter: func(t *testing.T, c chan string) {
s := <-c
if s != "Test1" {
t.Fatalf("Failed to get the correct value: Got = %s Expected = %s",
s, "Test1")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
c := make(chan string, 2)
wg.Add(1)
tt.fnBefore(t, c)
go recordGoroutine(c, ctx, &wg, tt.msg, STORE_WAIT)
time.Sleep(STORE_WAIT * 3)
cancel()
tt.fnAfter(t, c)
close(c)
wg.Wait()
})
}
}
func Test_getRecorder(t *testing.T) {
tests := []struct {
name string
t time.Duration
doRecord func(t *testing.T, rec recorderFn)
verify func(t *testing.T, c chan string)
}{
{
name: "Working Record",
t: STORE_WAIT * 2,
doRecord: func(t *testing.T, rec recorderFn) {
rec("Test1", "Test2")
},
verify: func(t *testing.T, c chan string) {
s := <-c
if !strings.Contains(s, "Test1,Test2") {
t.Fatalf("failed to find sub-string \n expected : %s\n got %s",
"Test1,Test2", s)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
c := make(chan string, 2)
rec := getRecorder(c, ctx, &wg, tt.t)
tt.doRecord(t, rec)
time.Sleep(STORE_WAIT * 3)
cancel()
tt.verify(t, c)
close(c)
wg.Wait()
})
}
}
func Test_Storage(t *testing.T) {
tests := []struct {
name string
record [2]string
check string
}{
{
name: "Basic Test",
record: [2]string{"Test1", "Test2"},
check: ",Test1,Test2",
},
{
name: "Quote Replace Test",
record: [2]string{"\"Test1\"", "Test2"},
check: `,"""Test1""",Test2`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
c := make(chan string, 2)
// Setup Writer
wg.Add(1)
go storeGoroutine(c, ctx, &wg, TEST_FILE)
// Get Writable Function
rec := getRecorder(c, ctx, &wg, STORE_WAIT)
// Wait and Send data
time.Sleep(STORE_WAIT / 2)
rec(tt.record[0], tt.record[1])
time.Sleep(STORE_WAIT * 3)
cancel()
close(c)
wg.Wait()
defer os.Remove(TEST_FILE)
// Read back the file for checking.
buf, err := os.ReadFile(TEST_FILE)
if err != nil {
t.Fatalf("failed to read the test file.")
}
if !strings.Contains(string(buf), tt.check) {
t.Errorf("failed to find the string %q", tt.check)
}
})
}
}