Skip to content

Iproto Server panics on pings #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
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