Skip to content

Commit

Permalink
add ci and put test
Browse files Browse the repository at this point in the history
  • Loading branch information
roseduan committed Aug 13, 2023
1 parent f78bea3 commit a68f09a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Go

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:

ubuntu-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19

- name: Run Go Vet
run: |
go vet ./...
- name: Run Go Fmt
run: |
files=$(go fmt ./...)
if [ -n "$files" ]; then
echo "Please run gofmt on these files ..."
echo "$files"
exit 1
fi
- name: Build
run: go build -v

- name: Run Unit Test
run: go test -count 1 -v ./...

windows-test:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19

- name: Run Go Vet
run: |
go vet ./...
- name: Build
run: go build -v

- name: Run Unit Test
run: go test -count 1 -v ./...
17 changes: 17 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type tableMeta struct {
}

func Open(options Options) (*Table, error) {
if err := checkOptions(options); err != nil {
return nil, err
}

t := &Table{
mu: new(sync.RWMutex),
options: options,
Expand Down Expand Up @@ -82,6 +86,19 @@ func Open(options Options) (*Table, error) {
return t, nil
}

func checkOptions(options Options) error {
if options.DirPath == "" {
return errors.New("dir path cannot be empty")
}
if options.SlotValueLength <= 0 {
return errors.New("slot value length must be greater than 0")
}
if options.LoadFactor < 0 || options.LoadFactor > 1 {
return errors.New("load factor must be between 0 and 1")
}
return nil
}

func (t *Table) readMeta() error {
// init meta file if not exist
if t.metaFile == nil {
Expand Down
67 changes: 67 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package diskhash

import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)

func destroyTable(t *Table) {
_ = t.Close()
_ = os.RemoveAll(t.options.DirPath)
}

func GetTestKey(i int) []byte {
return []byte(fmt.Sprintf("diskhash-test-key-%09d", i))
}

func TestOpen(t *testing.T) {
dir, err := os.MkdirTemp("", "diskhash-test-open")
assert.Nil(t, err)

options := DefaultOptions
options.DirPath = dir
options.SlotValueLength = 10
table, err := Open(options)
assert.Nil(t, err)
defer destroyTable(table)

err = table.Close()
assert.Nil(t, err)
}

func TestTable_Put(t *testing.T) {
t.Run("16B-10000000", func(t *testing.T) {
testTablePut(t, 16, 10000000)
})
t.Run("20B-2000000", func(t *testing.T) {
testTablePut(t, 16, 2000000)
})
t.Run("1K-500000", func(t *testing.T) {
testTablePut(t, 1024, 500000)
})
t.Run("4K-500000", func(t *testing.T) {
testTablePut(t, 4*1024, 500000)
})
}

func testTablePut(t *testing.T, valueLen uint32, count int) {
dir, err := os.MkdirTemp("", "diskhash-test-put")
assert.Nil(t, err)

options := DefaultOptions
options.DirPath = dir
options.SlotValueLength = valueLen
table, err := Open(options)
assert.Nil(t, err)
defer destroyTable(table)

for i := 0; i < count; i++ {
err = table.Put(GetTestKey(i), []byte(strings.Repeat("D", int(valueLen))), func(slot Slot) (bool, error) {
return false, nil
})
assert.Nil(t, err)
}
}

0 comments on commit a68f09a

Please sign in to comment.