-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoneroger_test.go
165 lines (144 loc) · 3.59 KB
/
moneroger_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
package moneroger
import (
"context"
"os"
"testing"
"time"
"github.com/opd-ai/moneroger/util"
)
// createTestConfig creates a test configuration with temporary directories
func createTestConfig(t *testing.T) util.Config {
t.Helper()
// Create temporary directory for data
dataDir, err := os.MkdirTemp("", "moneroger-test-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(dataDir) })
// Create temporary wallet file
walletFile, err := os.CreateTemp(dataDir, "wallet-*.keys")
if err != nil {
t.Fatal(err)
}
walletFile.Close()
return util.Config{
DataDir: dataDir,
WalletFile: walletFile.Name(),
MoneroPort: 18081,
WalletPort: 18082,
TestNet: true, // Use testnet for tests
}
}
// TestNewMoneroger tests the creation of a new Moneroger instance
func TestNewMoneroger(t *testing.T) {
tests := []struct {
name string
config util.Config
wantErr bool
}{
{
name: "valid config",
config: createTestConfig(t),
wantErr: false,
},
{
name: "invalid daemon port",
config: util.Config{
DataDir: "testdata",
WalletFile: "testdata/wallet.keys",
MoneroPort: -1, // Invalid port
WalletPort: 18082,
},
wantErr: true,
},
{
name: "missing wallet file",
config: util.Config{
DataDir: "testdata",
WalletFile: "", // Missing wallet file
MoneroPort: 18081,
WalletPort: 18082,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m, err := NewMoneroger(tt.config)
if (err != nil) != tt.wantErr {
t.Errorf("NewMoneroger() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
if m == nil {
t.Error("NewMoneroger() returned nil manager without error")
}
// Clean up
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = m.Shutdown(ctx)
}
})
}
}
// TestStartupShutdownSequence tests the proper ordering of operations
func TestStartupShutdownSequence(t *testing.T) {
config := createTestConfig(t)
m, err := NewMoneroger(config)
if err != nil {
t.Fatal(err)
}
// Test startup
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := m.Start(ctx); err != nil {
t.Errorf("Start() error = %v", err)
}
// Test shutdown
if err := m.Shutdown(ctx); err != nil {
t.Errorf("Shutdown() error = %v", err)
}
}
// TestContextCancellation verifies context handling
func TestContextCancellation(t *testing.T) {
config := createTestConfig(t)
m, err := NewMoneroger(config)
if err != nil {
t.Fatal(err)
}
// Create cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
// Start should respect context cancellation
if err := m.Start(ctx); err == nil {
t.Error("Start() should fail with cancelled context")
}
// Shutdown should still work with new context
ctx = context.Background()
if err := m.Shutdown(ctx); err != nil {
t.Errorf("Shutdown() error = %v", err)
}
}
// TestConcurrentAccess verifies thread safety
func TestConcurrentAccess(t *testing.T) {
config := createTestConfig(t)
m, err := NewMoneroger(config)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Start in goroutine
errChan := make(chan error, 1)
go func() {
errChan <- m.Start(ctx)
}()
// Attempt immediate shutdown
if err := m.Shutdown(ctx); err != nil {
t.Errorf("Concurrent Shutdown() error = %v", err)
}
// Check start result
if err := <-errChan; err != nil {
t.Errorf("Concurrent Start() error = %v", err)
}
}