-
Notifications
You must be signed in to change notification settings - Fork 1
/
lifo_disk_queue_test.go
91 lines (88 loc) · 2.38 KB
/
lifo_disk_queue_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
package queue
import (
"context"
"errors"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestNewLifoDiskQueue1(t *testing.T) {
file, err := ioutil.TempFile("", "")
if err != nil {
panic(err)
}
defer os.RemoveAll(file.Name())
file.Close()
queue, err := NewLifoDiskQueue(file.Name())
if err != nil {
panic(err)
}
name := "TestNewLifoDiskQueue"
{
if length := queue.Len(); length != 0 {
t.Error(name, "空队列-获取长度为0", length)
}
if data, err := queue.Get(nil); data != nil || !errors.Is(err, ErrQueueEmpty) {
t.Error(name, "空队列-Get数据返回ErrQueueEmpty", data, err)
}
}
{
if err := queue.Put(nil, []byte{}); err != nil {
t.Error(name, "空队列-Put数据返回nil", err)
}
if data, err := queue.Get(nil); !reflect.DeepEqual(data, []byte{}) || err != nil {
t.Error(name, "非空队列-Get数据返回nil", data, err)
}
}
{
if err := queue.Close(); err != nil {
t.Error(name, "队列关闭返回nil", err)
}
}
{
if data, err := queue.Get(nil); data != nil || !errors.Is(err, ErrQueueClosed) {
t.Error(name, "关闭队列-Get数据返回ErrQueueClosed", data, err)
}
if data, err := queue.Get(context.Background()); data != nil || !errors.Is(err, ErrQueueClosed) {
t.Error(name, "关闭队列-Get数据返回ErrQueueClosed", data, err)
}
if err := queue.Put(nil, []byte{}); !errors.Is(err, ErrQueueClosed) {
t.Error(name, "关闭队列-推送数据返回ErrQueueClosed", err)
}
if err := queue.Put(context.Background(), []byte{}); !errors.Is(err, ErrQueueClosed) {
t.Error(name, "关闭队列-推送数据返回ErrQueueClosed", err)
}
}
{
if err := queue.Close(); err != nil {
t.Error(name, "队列关闭返回nil", err)
}
}
}
func TestNewLifoDiskQueue2(t *testing.T) {
file, err := ioutil.TempFile("", "")
if err != nil {
panic(err)
}
defer os.RemoveAll(file.Name())
file.Close()
queue, err := NewLifoDiskQueue(file.Name())
if err != nil {
panic(err)
}
name := "TestNewLifoDiskQueue2"
if err := queue.Put(nil, []byte{}); err != nil {
t.Error(name, "空队列-Put数据返回nil", err)
}
if err := queue.Close(); err != nil {
t.Error(name, "队列关闭返回nil", err)
}
queue, err = NewLifoDiskQueue(file.Name())
if err != nil {
panic(err)
}
if data, err := queue.Get(nil); !reflect.DeepEqual(data, []byte{}) || err != nil {
t.Error(name, "非空队列-Get数据返回nil", data, err)
}
}