-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
158 lines (147 loc) · 4.58 KB
/
queue_test.go
File metadata and controls
158 lines (147 loc) · 4.58 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
151
152
153
154
155
156
157
158
package ojs
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestListQueues(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
if r.URL.Path != "/ojs/v1/queues" {
t.Errorf("expected /ojs/v1/queues, got %s", r.URL.Path)
}
w.Header().Set("Content-Type", ojsContentType)
json.NewEncoder(w).Encode(map[string]any{
"queues": []map[string]any{
{"name": "default", "status": "active", "created_at": "2026-01-01T00:00:00Z"},
{"name": "email", "status": "active", "created_at": "2026-01-01T00:00:00Z"},
},
"pagination": map[string]any{
"total": 2, "limit": 50, "offset": 0, "has_more": false,
},
})
}))
defer server.Close()
client, _ := NewClient(server.URL)
queues, pagination, err := client.ListQueues(context.Background())
if err != nil {
t.Fatalf("ListQueues() error = %v", err)
}
if len(queues) != 2 {
t.Fatalf("expected 2 queues, got %d", len(queues))
}
if queues[0].Name != "default" {
t.Errorf("expected first queue name=default, got %s", queues[0].Name)
}
if queues[1].Name != "email" {
t.Errorf("expected second queue name=email, got %s", queues[1].Name)
}
if pagination.Total != 2 {
t.Errorf("expected pagination total=2, got %d", pagination.Total)
}
if pagination.Limit != 50 {
t.Errorf("expected pagination limit=50, got %d", pagination.Limit)
}
}
func TestGetQueueStats(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ojs/v1/queues/email/stats" {
t.Errorf("expected /ojs/v1/queues/email/stats, got %s", r.URL.Path)
}
w.Header().Set("Content-Type", ojsContentType)
json.NewEncoder(w).Encode(map[string]any{
"queue": "email",
"status": "active",
"stats": map[string]any{
"available": 15,
"active": 3,
"scheduled": 2,
"retryable": 1,
"discarded": 0,
"completed_last_hour": 120,
"failed_last_hour": 5,
"avg_duration_ms": 245.5,
"avg_wait_ms": 12.3,
"throughput_per_second": 2.0,
},
"computed_at": "2026-02-12T10:00:00Z",
})
}))
defer server.Close()
client, _ := NewClient(server.URL)
stats, err := client.GetQueueStats(context.Background(), "email")
if err != nil {
t.Fatalf("GetQueueStats() error = %v", err)
}
if stats.Queue != "email" {
t.Errorf("expected queue=email, got %s", stats.Queue)
}
if stats.Available != 15 {
t.Errorf("expected available=15, got %d", stats.Available)
}
if stats.Active != 3 {
t.Errorf("expected active=3, got %d", stats.Active)
}
if stats.CompletedLastHour != 120 {
t.Errorf("expected completed_last_hour=120, got %d", stats.CompletedLastHour)
}
if stats.ThroughputPerSec != 2.0 {
t.Errorf("expected throughput=2.0, got %f", stats.ThroughputPerSec)
}
}
func TestPauseQueue(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/ojs/v1/queues/email/pause" {
t.Errorf("expected /ojs/v1/queues/email/pause, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client, _ := NewClient(server.URL)
err := client.PauseQueue(context.Background(), "email")
if err != nil {
t.Fatalf("PauseQueue() error = %v", err)
}
}
func TestResumeQueue(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/ojs/v1/queues/email/resume" {
t.Errorf("expected /ojs/v1/queues/email/resume, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client, _ := NewClient(server.URL)
err := client.ResumeQueue(context.Background(), "email")
if err != nil {
t.Fatalf("ResumeQueue() error = %v", err)
}
}
func TestPauseQueueError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ojsContentType)
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{
"code": "not_found",
"message": "Queue 'nonexistent' not found.",
},
})
}))
defer server.Close()
client, _ := NewClient(server.URL)
err := client.PauseQueue(context.Background(), "nonexistent")
if err == nil {
t.Fatal("expected error for nonexistent queue")
}
}