Skip to content

Commit

Permalink
some new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartek Rybak committed Feb 7, 2021
1 parent 7d6ec2a commit fe80cc7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions mutex/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,5 @@ func writeCurrentTimestamp(f *os.File) (int64, error) {
return timestamp, err
}
return timestamp, nil

}
55 changes: 55 additions & 0 deletions mutex/mutex_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mutex

import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -78,3 +80,56 @@ func TestSimpleMutexN(t *testing.T) {
t.Fatalf("wrong value %d instead of %d", value, want)
}
}

func TestLockPath(t *testing.T) {
const mutexId = "simple-test-mutex"
mutexRoot := temporaryCatalog(t)
mx, err := NewMutex(mutexRoot, mutexId)
if err != nil {
t.Fatalf("cannot create the mutex: %v", err)
}
want := filepath.Join(mutexRoot, mutexId, fmt.Sprintf("%s-mutex.lck", mutexId))
got := mx.LockPath()

if want != got {
t.Fatalf("wrong value \"%s\" instead of \"%s\"", got, want)
}
}

func TestId(t *testing.T) {
const mutexId = "simple-test-mutex"
mutexRoot := temporaryCatalog(t)
mx, err := NewMutex(mutexRoot, mutexId)
if err != nil {
t.Fatalf("cannot create the mutex: %v", err)
}
want := mutexId
got := mx.Id()

if want != got {
t.Fatalf("wrong value \"%s\" instead of \"%s\"", got, want)
}
}

func TestWhen(t *testing.T) {
const mutexId = "simple-test-mutex"
mutexRoot := temporaryCatalog(t)
mx, err := NewMutex(mutexRoot, mutexId)
if err != nil {
t.Fatalf("cannot create the mutex: %v", err)
}
mx.Lock()
defer mx.Unlock()
if file, err := os.Create(mx.LockPath()); err != nil {
t.Fatalf("cannot create the mutex file: %v", err)
} else {
if want, err := writeCurrentTimestamp(file); err != nil {
t.Fatalf("cannot write the timestamp: %v", err)
} else {
got := mx.When().UnixNano() / int64(time.Millisecond)
if want != got {
t.Fatalf("wrong value %d instead of %d", got, want)
}
}
}
}

0 comments on commit fe80cc7

Please sign in to comment.