Skip to content

Commit

Permalink
Add index test and use t.TempDir instead of ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMatthes committed Apr 29, 2024
1 parent 6c9bcbb commit d733152
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
18 changes: 4 additions & 14 deletions histories/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package histories

import (
"fmt"
"io/ioutil"
"predictor/env"
"predictor/observations"
"sync"
Expand All @@ -12,10 +11,7 @@ import (

func TestHistoryFileConcurrentWriteAndLoad(t *testing.T) {
var concurrent uint = 0
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf(err.Error())
}
tempDir := t.TempDir()
mockFilePath := fmt.Sprintf("%s/h.json", tempDir)

var wg sync.WaitGroup
Expand Down Expand Up @@ -43,10 +39,7 @@ func TestHistoryFileConcurrentWriteAndLoad(t *testing.T) {

func TestBypassCache(t *testing.T) {
var runs uint = 0
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf(err.Error())
}
tempDir := t.TempDir()
mockFilePath := fmt.Sprintf("%s/h.json", tempDir)

for {
Expand All @@ -70,10 +63,7 @@ func TestBypassCache(t *testing.T) {
}

func TestLoadBestHistory(t *testing.T) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf(err.Error())
}
tempDir := t.TempDir()
env.StaticPath = tempDir

unspecificHistoryFile := fmt.Sprintf("%s/history/%s", tempDir, "1337_1.json")
Expand All @@ -84,7 +74,7 @@ func TestLoadBestHistory(t *testing.T) {
specificHistoryCycle1 := HistoryCycle{
StartTime: time.Now(),
}
_, err = appendToHistoryFile(unspecificHistoryFile, unspecificHistoryCycle1)
_, err := appendToHistoryFile(unspecificHistoryFile, unspecificHistoryCycle1)
if err != nil {
t.Errorf(err.Error())
t.FailNow()
Expand Down
88 changes: 88 additions & 0 deletions histories/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package histories

import (
"encoding/json"
"fmt"
"os"
"predictor/env"
"reflect"
"testing"
"time"
)

func TestHistoryIndex(t *testing.T) {
// Write some dummy histories in the cache.
tempDir := t.TempDir()
exampleCycle := HistoryCycle{
StartTime: time.Unix(0, 0),
EndTime: time.Unix(10, 0), // Usually cycles are longer, but suffices for testing
Phases: []HistoryPhaseEvent{
// Green at 0 seconds
{
Time: time.Unix(0, 0),
Color: 3,
},
// Red at 5 seconds
{
Time: time.Unix(5, 0),
Color: 1,
},
},
}
history := History{
Cycles: []HistoryCycle{
exampleCycle,
exampleCycle,
},
}
jsonData, err := json.Marshal(history)
if err != nil {
t.Errorf("failed to marshal json data: %s", err.Error())
t.FailNow()
}
historyFilePath := fmt.Sprintf("%s/1337_1.json", tempDir)
historyFile, err := os.Create(historyFilePath)
if err != nil {
t.Errorf("history file could not be created: %s", err.Error())
t.FailNow()
}
defer historyFile.Close()
_, err = historyFile.Write(jsonData)
if err != nil {
t.Errorf("could not write into history file: %s", err.Error())
t.FailNow()
}
cache.Store(historyFilePath, history)

env.StaticPath = tempDir
UpdateHistoryIndex()

indexFilePath := fmt.Sprintf("%s/index.json", tempDir)
indexFile, err := os.OpenFile(indexFilePath, os.O_RDONLY, 0644)
if err != nil {
t.Errorf("could not open index file: %s", err.Error())
t.FailNow()
}
defer indexFile.Close()

decoder := json.NewDecoder(indexFile)
var unmarshaledIndex []IndexEntry
err = decoder.Decode(&unmarshaledIndex)
if err != nil {
t.Errorf("could not unmarshal data from index file: %s", err.Error())
t.FailNow()
}

expectedIndex := []IndexEntry{
{
File: "1337_1.json",
LastUpdated: time.Unix(10, 0),
CycleCount: 2,
},
}

// Using deep equals here is fine
if !reflect.DeepEqual(unmarshaledIndex, expectedIndex) {
t.Errorf("expected index does not correspond with unmarshaled index")
}
}

0 comments on commit d733152

Please sign in to comment.