Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Nov 26, 2023
1 parent 8053fbf commit a733438
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion artifactory/commands/transferfiles/state/statemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func (ts *TransferStateManager) tryLockStateManager() error {
return nil
}

func (ts *TransferStateManager) Running() (isRunning bool, err error) {
func (ts *TransferStateManager) Running() (running bool, err error) {
lockDirPath, err := coreutils.GetJfrogTransferLockDir()
if err != nil {
return false, err
Expand Down
75 changes: 75 additions & 0 deletions artifactory/commands/transferfiles/state/statemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,78 @@ func TestTryLockStateManager(t *testing.T) {
assert.NoError(t, stateManager.tryLockStateManager())
assert.ErrorIs(t, new(AlreadyLockedError), stateManager.tryLockStateManager())
}

func TestRunning(t *testing.T) {
stateManager, cleanUp := InitStateTest(t)
defer cleanUp()

// Assert no running=false
running, err := stateManager.Running()
assert.NoError(t, err)
assert.False(t, running)

// Lock to simulate transfer
assert.NoError(t, stateManager.TryLockTransferStateManager())

// Assert running=true
running, err = stateManager.Running()
assert.NoError(t, err)
assert.True(t, running)
}

func TestInitStartTimestamp(t *testing.T) {
stateManager, cleanUp := InitStateTest(t)
defer cleanUp()

// Init start timestamp and expect timestamp zero
running, err := stateManager.InitStartTimestamp()
assert.NoError(t, err)
assert.False(t, running)
assert.True(t, stateManager.startTimestamp.IsZero())

// Lock to simulate transfer
assert.NoError(t, stateManager.TryLockTransferStateManager())

// Init start timestamp and expect timestamp non-zero
running, err = stateManager.InitStartTimestamp()
assert.NoError(t, err)
assert.True(t, running)
assert.False(t, stateManager.startTimestamp.IsZero())
}

var getRunningTimeStringCases = []struct {
startTimestamp time.Time
expectedString string
}{
{time.Now(), "Less than a minute"},
{time.Now().Add(-time.Second), "Less than a minute"},
{time.Now().Add(-time.Minute), "1 minute"},
{time.Now().Add(-time.Hour), "1 hour"},
{time.Now().Add(-time.Hour).Add(time.Minute), "59 minutes"},
{time.Now().Add(-time.Hour).Add(time.Minute).Add(time.Second), "58 minutes"},
}

func TestGetRunningTimeString(t *testing.T) {
stateManager, cleanUp := InitStateTest(t)
defer cleanUp()

runningTime := stateManager.GetRunningTimeString()
assert.Empty(t, runningTime)

// Lock and init start timestamp to simulate transfer
assert.NoError(t, stateManager.TryLockTransferStateManager())
running, err := stateManager.InitStartTimestamp()
assert.NoError(t, err)
assert.True(t, running)

// Run test cases
for _, testCase := range getRunningTimeStringCases {
t.Run(testCase.startTimestamp.String(), func(t *testing.T) {
// Set start timestamp
stateManager.startTimestamp = testCase.startTimestamp

// Assert running time string
assert.Equal(t, testCase.expectedString, stateManager.GetRunningTimeString())
})
}
}

0 comments on commit a733438

Please sign in to comment.