Skip to content

Commit

Permalink
test race fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed Mar 31, 2023
1 parent 86a452e commit 16baa33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif
format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*/generated/*" | xargs gofmt -w -s
test:
go test ./... -v -race -cpu=1,2,4,8 -coverprofile=coverage.txt -covermode=atomic -benchmem -bench .
go test ./... -v -race -cpu=1,2 -coverprofile=coverage.txt -covermode=atomic -benchmem -bench .
clean:
rm -rf coverage.txt
go clean -cache
49 changes: 25 additions & 24 deletions filecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ func TestCacheStartStop(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()
time.Sleep(1 * time.Second)
cache.Stop()
fmt.Println("ok")
}

Expand All @@ -72,7 +73,9 @@ func TestTimeExpiration(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()
name := "expired"
itm := getTimeExpiredCacheItem()
cache._add_cache_item(name, itm)
Expand All @@ -83,7 +86,6 @@ func TestTimeExpiration(t *testing.T) {
} else {
fmt.Println("ok")
}
cache.Stop()
}

func TestTimeExpirationUpdate(t *testing.T) {
Expand All @@ -94,13 +96,14 @@ func TestTimeExpirationUpdate(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()
testFile := "filecache.go"
cache.CacheNow(testFile)
if !cache.InCache(testFile) {
fmt.Println("failed")
fmt.Println("[!] failed to cache file")
cache.Stop()
t.FailNow()
}
time.Sleep(1500 * time.Millisecond)
Expand All @@ -113,7 +116,6 @@ func TestTimeExpirationUpdate(t *testing.T) {
} else {
fmt.Println("cache contents do not match file")
}
cache.Stop()
t.FailNow()
}
time.Sleep(1 * time.Second)
Expand All @@ -124,7 +126,6 @@ func TestTimeExpirationUpdate(t *testing.T) {
} else {
fmt.Println("ok")
}
cache.Stop()
}

func TestFileChanged(t *testing.T) {
Expand All @@ -133,24 +134,23 @@ func TestFileChanged(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

name := writeTempFile(t, "lorem ipsum blah blah")
if name == "" {
fmt.Println("failed!")
fmt.Println("[!] failed to cache item")
cache.Stop()
t.FailNow()
} else if err := cache.CacheNow(name); err != nil {
fmt.Println("failed!")
fmt.Println("[!] failed to cache item")
cache.Stop()
t.FailNow()
} else if !cache.InCache(name) {
fmt.Println("failed")
fmt.Println("[!] failed to cache item")
os.Remove(name)
cache.Stop()
t.FailNow()
}
time.Sleep(1 * time.Second)
Expand All @@ -165,7 +165,6 @@ func TestFileChanged(t *testing.T) {
t.Fail()
}
os.Remove(name)
cache.Stop()
fmt.Println("ok")
}

Expand All @@ -175,16 +174,16 @@ func TestCache(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()
name := writeTempFile(t, "lorem ipsum akldfjsdlf")
if name == "" {
cache.Stop()
t.FailNow()
} else if cache.InCache(name) {
fmt.Println("failed")
fmt.Println("[!] item should not be in cache yet!")
os.Remove(name)
cache.Stop()
t.FailNow()
}

Expand Down Expand Up @@ -218,9 +217,7 @@ func TestCache(t *testing.T) {
fmt.Println("ok")
fmt.Printf("\t[*] item cached in %dµs\n", delay)
}
cache.Stop()
os.Remove(name)

}

func TestExpireAll(t *testing.T) {
Expand All @@ -231,16 +228,16 @@ func TestExpireAll(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

name := writeTempFile(t, "this is a first file and some stuff should go here")
if name == "" {
cache.Stop()
t.Fail()
}
name2 := writeTempFile(t, "this is the second file")
if name2 == "" {
cache.Stop()
os.Remove(name)
t.Fail()
}
Expand Down Expand Up @@ -274,7 +271,6 @@ func TestExpireAll(t *testing.T) {
}
os.Remove(name)
os.Remove(name2)
cache.Stop()
}

func destroyNames(names []string) {
Expand All @@ -290,7 +286,9 @@ func TestExpireOldest(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

names := make([]string, 0)
for i := 0; i < 1000; i++ {
Expand All @@ -311,7 +309,6 @@ func TestExpireOldest(t *testing.T) {
if !t.Failed() {
fmt.Println("ok")
}
cache.Stop()
destroyNames(names)
}

Expand All @@ -323,7 +320,9 @@ func TestNeverExpire(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

tmpf, err := os.CreateTemp("", "fctest")
if err != nil {
Expand All @@ -339,7 +338,6 @@ func TestNeverExpire(t *testing.T) {
fmt.Println("failed")
fmt.Println("[!] couldn't write temporary file: ", err.Error())
os.Remove(name)
cache.Stop()
t.FailNow()
}
cache.Cache(name, nil)
Expand All @@ -351,7 +349,6 @@ func TestNeverExpire(t *testing.T) {
} else {
fmt.Println("ok")
}
cache.Stop()
os.Remove(name)
}

Expand All @@ -360,7 +357,9 @@ func BenchmarkAsyncCaching(b *testing.B) {
cache := NewDefaultCache()
if err := cache.Start(); err != nil {
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

cache.Cache("filecache.go", nil)
for {
Expand All @@ -370,7 +369,6 @@ func BenchmarkAsyncCaching(b *testing.B) {
<-time.After(200 * time.Microsecond)
}
cache.Remove("filecache.go")
cache.Stop()
}
}

Expand All @@ -381,12 +379,13 @@ func TestCacheReadFile(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

if cache.InCache(testFile) {
fmt.Println("failed")
fmt.Println("[!] file should not be in cache yet")
cache.Stop()
t.FailNow()
}

Expand All @@ -399,7 +398,6 @@ func TestCacheReadFile(t *testing.T) {
} else {
fmt.Println("file does not match cache contents")
}
cache.Stop()
t.FailNow()
}

Expand All @@ -412,7 +410,6 @@ func TestCacheReadFile(t *testing.T) {
} else {
fmt.Println("ok")
}
cache.Stop()
}

func BenchmarkSyncCaching(b *testing.B) {
Expand All @@ -421,10 +418,11 @@ func BenchmarkSyncCaching(b *testing.B) {

if err := cache.Start(); err != nil {
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()
cache.CacheNow("filecache.go")
cache.Remove("filecache.go")
cache.Stop()
}
}

Expand All @@ -451,7 +449,10 @@ func TestAccessCount(t *testing.T) {
if err := cache.Start(); err != nil {
fmt.Println("failed")
fmt.Println("[!] cache failed to start: ", err.Error())
return
}
defer cache.Stop()

for i := 0; i < count; i++ {
name := strconv.Itoa(i)
itm := getTimeExpiredCacheItem()
Expand Down

0 comments on commit 16baa33

Please sign in to comment.