Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const saltSize = 32
type QueryHandler func(queryContext context.Context, query Query) *Result
type OnShutdownCallback func(err error)

func defaultPingStatus(*IprotoServer) uint { return OKCommand }

type IprotoServer struct {
sync.Mutex
conn net.Conn
Expand Down Expand Up @@ -43,13 +45,14 @@ type IprotoServerOptions struct {

func NewIprotoServer(uuid string, handler QueryHandler, onShutdown OnShutdownCallback) *IprotoServer {
return &IprotoServer{
conn: nil,
reader: nil,
writer: nil,
handler: handler,
onShutdown: onShutdown,
uuid: uuid,
schemaID: 1,
conn: nil,
reader: nil,
writer: nil,
handler: handler,
onShutdown: onShutdown,
uuid: uuid,
schemaID: 1,
getPingStatus: defaultPingStatus,
}
}

Expand All @@ -58,9 +61,8 @@ func (s *IprotoServer) WithOptions(opts *IprotoServerOptions) *IprotoServer {
opts = &IprotoServerOptions{}
}
s.perf = opts.Perf
s.getPingStatus = opts.GetPingStatus
if s.getPingStatus == nil {
s.getPingStatus = func(*IprotoServer) uint { return 0 }
if opts.GetPingStatus != nil {
s.getPingStatus = opts.GetPingStatus
}
return s
}
Expand Down
44 changes: 44 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tarantool

import (
"context"
"net"
"testing"

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

func TestServerPing(t *testing.T) {
handler := func(queryContext context.Context, query Query) *Result {
return &Result{}
}

s := NewIprotoServer("1", handler, nil)

listenAddr := make(chan string)
go func() {
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer ln.Close()

listenAddr <- ln.Addr().String()
close(listenAddr)

conn, err := ln.Accept()
require.NoError(t, err)

s.Accept(conn)
}()

addr := <-listenAddr
conn, err := Connect(addr, nil)
require.NoError(t, err)

res := conn.Exec(context.Background(), &Ping{})
assert.Equal(t, res.ErrorCode, OKCommand)
assert.NoError(t, res.Error)

conn.Close()
s.Shutdown()
}
Loading