Skip to content

Commit

Permalink
fix(test): failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ehrktia committed Jun 29, 2024
1 parent 50cfbec commit 2b5654b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
23 changes: 10 additions & 13 deletions datastructure/datastructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ package datastructure

import (
"fmt"
"sync"
"strconv"
"testing"
)

func Test_get_all(t *testing.T) {
s := 5
once := &sync.Once{}
NewQueue(once, s)
setup()
qSize, err := strconv.Atoi(size)
if err != nil {
panic("error creating new test cache")
}
// test add
for i := 1; i < s; i++ {
for i := 1; i < qSize; i++ {
key := fmt.Sprintf("%s-%d", t.Name(), i)
value := fmt.Sprintf("%d", i)
r, l := Add(key, value)
t.Logf("r:%v\n", r)
t.Logf("loaded:%t\n", l)
_, _ = Add(key, value)
}
// test get
val := GetAll()
for k, v := range val {
t.Logf("key:%v\tval:%v\n", k, v)
}
if len(val) != s-1 {
t.Fatalf("expected-%d,got-%d\n", s-1, len(val))
if len(val) != qSize-1 {
t.Fatalf("expected-%d,got-%d\n", qSize-1, len(val))
}

}
49 changes: 3 additions & 46 deletions datastructure/struct_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datastructure

import (
"fmt"
"os"
"strconv"
"sync"
Expand All @@ -10,7 +9,8 @@ import (

var size = "5"

func setup(once *sync.Once) {
func setup() {
once := &sync.Once{}
os.Setenv("QUEUE_SIZE", size)
i, err := strconv.Atoi(size)
if err != nil {
Expand All @@ -21,50 +21,7 @@ func setup(once *sync.Once) {

func Test_conc_map(t *testing.T) {
// setup the queue
once := &sync.Once{}
setup(once)
t.Run("add new element to cache", func(t *testing.T) {
key := t.Name()
value := fmt.Sprintf("%d", 1)
// test add
r,l:= Add(key, value)
updIdx := inMemoryIdx.lastAdded
if updIdx != 0 {
t.Fatalf("expected-%d\tgot-%d\n", 0, updIdx)
}
if r != "1" {
t.Fatalf("expected:%s\tgot:%s\n", "1", r)
}
if l {
t.Fatalf("expected:%t\tgot:%t\n", false, l)
}
if <-done {
t.Logf("completed")
}
if inMemoryIdx.list[0] != t.Name() {
t.Fatalf("expected-%s\tgot-%s\n", t.Name(), inMemoryIdx.list[0])
}
s, err := strconv.Atoi(size)
if err != nil {
t.Fatalf("expected:%q\tgot:%v\n", "nil", err)
}
if s != len(inMemoryIdx.list) {
t.Fatalf("expected:%d\tgot-%d\n", s, len(inMemoryIdx.list))
}
// test get
v := Get(key)
if v.(string) == NotFound {
t.Fatalf("expected-%q\tgot-%q\n", NotFound, v.(string))
}
// test delete
Delete(t.Name())
if inMemoryIdx.list[0] != nil {
t.Fatalf("expected:%q\tgot:%v\n", "nil", inMemoryIdx.list[0])
}
if inMemoryIdx.lastAdded != 0 {
t.Logf("expected:%d\tgot:%d\n", 0, inMemoryIdx.lastAdded)
}
})
setup()

t.Run("evict when queue is full", func(t *testing.T) {
tot := len(inMemoryIdx.list) - 1
Expand Down

0 comments on commit 2b5654b

Please sign in to comment.