fix: Update tests to handle autostart behavior

- TestStartMiner_Ugly: Add algorithm to config for consistent instance naming,
  ensuring duplicate detection works correctly
- TestListMiners_Good: Account for autostarted miners by checking delta instead
  of absolute count
- TestListMiners: Renamed from TestListMinersEmpty since autostart may add miners
- Add defer manager.Stop() to all tests in mining_test.go for proper cleanup

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
snider 2025-12-31 10:17:14 +00:00
parent b454bbd6d6
commit 749dd76f9c
2 changed files with 24 additions and 13 deletions

View file

@ -86,17 +86,20 @@ func TestStartMiner_Ugly(t *testing.T) {
m := setupTestManager(t)
defer m.Stop()
// Use an algorithm to get consistent instance naming (xmrig-test_algo)
// Without algo, each start gets a random suffix and won't be detected as duplicate
config := &Config{
HTTPPort: 9001, // Use a different port to avoid conflict
Pool: "test:1234",
Wallet: "testwallet",
Algo: "test_algo", // Consistent algo = consistent instance name
}
// Case 1: Successfully start a supported miner
_, err := m.StartMiner(context.Background(), "xmrig", config)
if err != nil {
t.Fatalf("Expected to start miner, but got error: %v", err)
}
// Case 3: Attempt to start a duplicate miner
// Case 3: Attempt to start a duplicate miner (same algo = same instance name)
_, err = m.StartMiner(context.Background(), "xmrig", config)
if err == nil {
t.Error("Expected an error when starting a duplicate miner, but got nil")
@ -174,22 +177,20 @@ func TestListMiners_Good(t *testing.T) {
m := setupTestManager(t)
defer m.Stop()
// Case 1: List miners when empty
miners := m.ListMiners()
if len(miners) != 0 {
t.Errorf("Expected 0 miners, but got %d", len(miners))
}
// Get initial count (may include autostarted miners from config)
initialMiners := m.ListMiners()
initialCount := len(initialMiners)
// Case 2: List miners when not empty
// Case 2: List miners after starting one - should have one more
config := &Config{
HTTPPort: 9004,
Pool: "test:1234",
Wallet: "testwallet",
}
_, _ = m.StartMiner(context.Background(), "xmrig", config)
miners = m.ListMiners()
if len(miners) != 1 {
t.Errorf("Expected 1 miner, but got %d", len(miners))
miners := m.ListMiners()
if len(miners) != initialCount+1 {
t.Errorf("Expected %d miners (initial %d + 1), but got %d", initialCount+1, initialCount, len(miners))
}
}

View file

@ -7,6 +7,8 @@ import (
func TestNewManager(t *testing.T) {
manager := NewManager()
defer manager.Stop()
if manager == nil {
t.Fatal("NewManager returned nil")
}
@ -17,6 +19,7 @@ func TestNewManager(t *testing.T) {
func TestStartAndStopMiner(t *testing.T) {
manager := NewManager()
defer manager.Stop()
config := &Config{
Pool: "pool.example.com",
@ -38,6 +41,7 @@ func TestStartAndStopMiner(t *testing.T) {
func TestGetNonExistentMiner(t *testing.T) {
manager := NewManager()
defer manager.Stop()
_, err := manager.GetMiner("non-existent")
if err == nil {
@ -45,16 +49,22 @@ func TestGetNonExistentMiner(t *testing.T) {
}
}
func TestListMinersEmpty(t *testing.T) {
func TestListMiners(t *testing.T) {
manager := NewManager()
defer manager.Stop()
// ListMiners should return a valid slice (may include autostarted miners)
miners := manager.ListMiners()
if len(miners) != 0 {
t.Errorf("Expected 0 miners, got %d", len(miners))
if miners == nil {
t.Error("ListMiners returned nil")
}
// Note: count may be > 0 if autostart is configured
}
func TestListAvailableMiners(t *testing.T) {
manager := NewManager()
defer manager.Stop()
miners := manager.ListAvailableMiners()
if len(miners) == 0 {
t.Error("Expected at least one available miner")