-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(loader): Change the running state from bool to atomic int32 …
…for thread safety Updated the Loader struct to use atomic operations for the running state, ensuring safe concurrent access. Modified the Start and Stop methods to reflect this change. Updated tests to check the running state correctly.
- Loading branch information
Showing
2 changed files
with
65 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,69 @@ | ||
package loader | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Start(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
waitTime time.Duration | ||
expect bool | ||
}{ | ||
{ | ||
name: "Running", | ||
waitTime: 50 * time.Millisecond, | ||
expect: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
loader := New() | ||
loader.Start() | ||
defer loader.Stop() | ||
|
||
time.Sleep(tt.waitTime) | ||
assert.Equal(t, tt.expect, loader.running) | ||
}) | ||
} | ||
func TestLoader_Start(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
waitTime time.Duration | ||
expectRunning bool | ||
}{ | ||
{ | ||
name: "Loader starts and runs", | ||
waitTime: 200 * time.Millisecond, | ||
expectRunning: true, | ||
}, | ||
{ | ||
name: "Loader runs for a short time", | ||
waitTime: 100 * time.Millisecond, | ||
expectRunning: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := New() | ||
|
||
l.Start() | ||
time.Sleep(tt.waitTime) | ||
|
||
running := atomic.LoadInt32(&l.running) == 1 | ||
assert.Equal(t, tt.expectRunning, running) | ||
|
||
l.Stop() | ||
}) | ||
} | ||
} | ||
|
||
func Test_Stop(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
waitTime time.Duration | ||
expect bool | ||
}{ | ||
{ | ||
name: "Stopped", | ||
waitTime: 50 * time.Millisecond, | ||
expect: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
loader := New() | ||
loader.Start() | ||
loader.Stop() | ||
|
||
time.Sleep(tt.waitTime) | ||
assert.Equal(t, tt.expect, loader.running) | ||
}) | ||
} | ||
func TestLoader_Stop(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
waitTime time.Duration | ||
expectRunning bool | ||
}{ | ||
{ | ||
name: "Loader stops after start", | ||
waitTime: 100 * time.Millisecond, | ||
expectRunning: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := New() | ||
|
||
l.Start() | ||
l.Stop() | ||
time.Sleep(tt.waitTime) | ||
|
||
running := atomic.LoadInt32(&l.running) == 1 | ||
assert.Equal(t, tt.expectRunning, running) | ||
}) | ||
} | ||
} |