Skip to content

Commit

Permalink
test add to actions memcached & add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yosh11 committed Jan 23, 2024
1 parent c6e3c40 commit 70d2240
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 2 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ jobs:
runs-on: ${{ matrix.os }}
permissions:
contents: write
services:
memcached:
image: memcached:latest
ports:
- 11211:11211
options: --entrypoint=""
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
Expand All @@ -30,7 +36,7 @@ jobs:
uses: tj-actions/coverage-badge-go@v1
if: ${{ runner.os == 'linux' }}
with:
green: 80
green: 70
filename: coverage.out

- uses: stefanzweifel/git-auto-commit-action@v5
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Gomemcached

---
<div align=center>
<img src="https://github.com/aliexpressru/gomemcached/raw/main/assets/logo.png" width="300"/>
Expand Down
5 changes: 4 additions & 1 deletion consistenthash/hashring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ func BenchmarkHashRingGet(b *testing.B) {
func TestHashRing_GetAllNodes(t *testing.T) {
ch := NewHashRing()

allNodes := ch.GetAllNodes()
assert.Nil(t, allNodes, "GetAllNodes: without added nodes")

for i := 0; i < keySize; i++ {
ch.Add("localhost:" + strconv.Itoa(i))
}
count := ch.GetNodesCount()
assert.Equalf(t, keySize, count, "GetNodesCount: have - %d; want - %d", count, keySize)

allNodes := ch.GetAllNodes()
allNodes = ch.GetAllNodes()
assert.Equal(t, keySize, len(allNodes))

for i := 0; i < keySize; i++ {
Expand Down
112 changes: 112 additions & 0 deletions memcached/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package memcached
import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCommandCodeString(t *testing.T) {
Expand Down Expand Up @@ -38,3 +40,113 @@ func TestIsQuiet(t *testing.T) {
}
}
}

func Test_prepareAuthData(t *testing.T) {
type args struct {
user string
pass string
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "1", args: args{
user: "testuser",
pass: "testpass",
},
want: []byte("\x00testuser\x00testpass"),
},
{
name: "2", args: args{
user: "anotheruser",
pass: "anotherpass",
},
want: []byte("\x00anotheruser\x00anotherpass"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, prepareAuthData(tt.args.user, tt.args.pass), "prepareAuthData(%v, %v)", tt.args.user, tt.args.pass)
})
}
}

func TestOpCode_changeOnQuiet(t *testing.T) {
type args struct {
def OpCode
}
tests := []struct {
name string
o OpCode
args args
want OpCode
}{
{
name: GET.String(),
o: GET,
args: args{def: GETQ},
want: GETQ,
},
{
name: SET.String(),
o: SET,
args: args{def: GETQ},
want: SETQ,
},
{
name: ADD.String(),
o: ADD,
args: args{def: GETQ},
want: ADDQ,
},
{
name: REPLACE.String(),
o: REPLACE,
args: args{def: GETQ},
want: REPLACEQ,
},
{
name: DELETE.String(),
o: DELETE,
args: args{def: GETQ},
want: DELETEQ,
},
{
name: INCREMENT.String(),
o: INCREMENT,
args: args{def: GETQ},
want: INCREMENTQ,
},
{
name: DECREMENT.String(),
o: DECREMENT,
args: args{def: GETQ},
want: DECREMENTQ,
},
{
name: FLUSH.String(),
o: FLUSH,
args: args{def: GETQ},
want: FLUSHQ,
},
{
name: APPEND.String(),
o: APPEND,
args: args{def: GETQ},
want: APPENDQ,
},
{
name: PREPEND.String(),
o: PREPEND,
args: args{def: GETQ},
want: PREPENDQ,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.o.changeOnQuiet(tt.args.def), "changeOnQuiet(%v)", tt.args.def)
})
}
}
50 changes: 50 additions & 0 deletions memcached/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package memcached

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/aliexpressru/gomemcached/consistenthash"
)

func TestWithOptions(t *testing.T) {
const (
maxIdleConns = 10
disable = true
enable
authUser = "admin"
authPass = "password"
timeout = 5 * time.Second
period = time.Second
)

hr := consistenthash.NewCustomHashRing(1, nil)
os.Setenv("MEMCACHED_SERVERS", "localhost:11211")
mcl, _ := InitFromEnv(
WithMaxIdleConns(maxIdleConns),
WithTimeout(timeout),
WithCustomHashRing(hr),
WithPeriodForNodeHealthCheck(period),
WithPeriodForRebuildingNodes(period),
WithDisableNodeProvider(),
WithDisableRefreshConnsInPool(),
WithDisableMemcachedDiagnostic(),
WithAuthentication(authUser, authPass),
)
t.Cleanup(func() {
mcl.CloseAllConns()
})

assert.Equal(t, maxIdleConns, mcl.maxIdleConns, "WithMaxIdleConns should set maxIdleConns")
assert.Equal(t, timeout, mcl.timeout, "WithTimeout should set timeout")
assert.Equal(t, hr, mcl.hr, "WithCustomHashRing should set hr")
assert.Equal(t, period, mcl.nodeHCPeriod, "WithPeriodForNodeHealthCheck should set period")
assert.Equal(t, period, mcl.nodeRBPeriod, "WithPeriodForRebuildingNodes should set period")
assert.Equal(t, disable, mcl.disableNodeProvider, "WithDisableNodeProvider should set disable")
assert.Equal(t, disable, mcl.disableRefreshConns, "WithDisableRefreshConnsInPool should set disable")
assert.Equal(t, disable, mcl.disableMemcachedDiagnostic, "WithDisableMemcachedDiagnostic should set disable")
assert.Equal(t, enable, mcl.authEnable, "WithAuthentication should set enable")
}
87 changes: 87 additions & 0 deletions memcached/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,90 @@ func BenchmarkReceiveRequestNoBuf(b *testing.B) {
}
}
}

func TestRequest_prepareExtras(t *testing.T) {
type fields struct {
Opcode OpCode
}
type args struct {
expiration uint32
delta uint64
initVal uint64
}
tests := []struct {
name string
fields fields
args args
expect []byte
}{
{
name: "GET must not have extras",
fields: fields{
Opcode: GET,
},
args: args{
expiration: 256,
delta: 1,
initVal: 1,
},
expect: nil,
},
{
name: "SET",
fields: fields{
Opcode: SET,
},
args: args{
expiration: 256,
delta: 1,
initVal: 1,
},
expect: []byte{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
},
},
{
name: "INCREMENT",
fields: fields{
Opcode: INCREMENT,
},
args: args{
expiration: 256,
delta: 1,
initVal: 42,
},
expect: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a,
0x00, 0x00, 0x01, 0x00,
},
},
{
name: "FLUSH",
fields: fields{
Opcode: FLUSH,
},
args: args{
expiration: 256,
delta: 0,
initVal: 0,
},
expect: []byte{
0x00, 0x00, 0x01, 0x00,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Request{
Opcode: tt.fields.Opcode,
}
r.prepareExtras(tt.args.expiration, tt.args.delta, tt.args.initVal)

if !bytes.Equal(r.Extras, tt.expect) {
t.Fatalf("Expected %#v == %#v", r.Extras, tt.expect)
}
})
}
}

0 comments on commit 70d2240

Please sign in to comment.