Skip to content

Commit

Permalink
feat: add SendWithContext (#1)
Browse files Browse the repository at this point in the history
* feat: add SendWithContext

* test: add test case for context cancel
  • Loading branch information
jonlundy authored Sep 27, 2022
1 parent 670ede5 commit 2279538
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
35 changes: 22 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package ws4sqlite_client

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -54,14 +55,12 @@ const (
//
// Example:
//
// cli, err := ws4.NewClientBuilder().
// WithURL("http://localhost:12321/db2").
// WithInlineAuth("myUser1", "myHotPassword").
// Build()
//
// cli.Send(...)
//
// cli, err := ws4.NewClientBuilder().
// WithURL("http://localhost:12321/db2").
// WithInlineAuth("myUser1", "myHotPassword").
// Build()
//
// cli.Send(...)
type ClientBuilder struct {
url string
authMode AuthMode
Expand All @@ -75,12 +74,12 @@ type ClientBuilder struct {
//
// Example:
//
// cli, err := ws4.NewClientBuilder().
// WithURL("http://localhost:12321/db2").
// WithInlineAuth("myUser1", "myHotPassword").
// Build()
// cli, err := ws4.NewClientBuilder().
// WithURL("http://localhost:12321/db2").
// WithInlineAuth("myUser1", "myHotPassword").
// Build()
//
// cli.Send(...)
// cli.Send(...)
type Client struct {
ClientBuilder
}
Expand Down Expand Up @@ -144,6 +143,16 @@ func (cb *ClientBuilder) Build() (*Client, error) {
// Returns a WsError if the remote service returns a processing error. If the
// communication fails, it returns the "naked" error, so check for cast-ability.
func (c *Client) Send(req *Request) (*Response, int, error) {
return c.SendWithContext(context.Background(), req)
}


// SendWithContext sends a set of requests to the remote with context, wrapped in a Request.
// Returns a matching set of responses, wrapped in a Response struct.
//
// Returns a WsError if the remote service returns a processing error. If the
// communication fails, it returns the "naked" error, so check for cast-ability.
func (c *Client) SendWithContext(ctx context.Context, req *Request) (*Response, int, error) {
if c.authMode == AUTH_MODE_INLINE {
req.req.Credentials = &credentials{
User: c.user,
Expand All @@ -157,7 +166,7 @@ func (c *Client) Send(req *Request) (*Response, int, error) {
}

client := &http.Client{}
post, err := http.NewRequest("POST", c.url, bytes.NewBuffer(jsonData))
post, err := http.NewRequestWithContext(ctx, "POST", c.url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, 0, err
}
Expand Down
40 changes: 39 additions & 1 deletion ws4sqlite_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package ws4sqlite_client_test

import (
"context"
"errors"
"os"
"os/exec"
"syscall"
Expand All @@ -34,7 +36,7 @@ func kill(cmd *exec.Cmd) {
}

func TestMain(m *testing.M) {
cmd := exec.Command("test/ws4sqlite-0.11.0", "--mem-db", "mydb:test/mydb.yaml", "--mem-db", "mydb2:test/mydb2.yaml")
cmd := exec.Command("test/ws4sqlite-0.12.2", "--mem-db", "mydb:test/mydb.yaml", "--mem-db", "mydb2:test/mydb2.yaml")

err := cmd.Start()
if err != nil {
Expand Down Expand Up @@ -244,3 +246,39 @@ func TestError(t *testing.T) {
t.Error("error is not 500")
}
}

func TestCancel(t *testing.T) {
client, err := ws4.NewClientBuilder().
WithURLComponents(ws4.PROTOCOL_HTTP, "localhost", 12321, "mydb2").
WithInlineAuth("myUser1", "myHotPassword").
Build()

if err != nil {
t.Error(err)
}

request, err := ws4.NewRequestBuilder().
AddQuery("SELENCT * FROM TEMP").
Build()

if err != nil {
t.Error(err)
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

_, code, err := client.SendWithContext(ctx, request)

if err == nil {
t.Error("did not fail, but should have")
}

if code == 200 {
t.Error("did return 200, but shouldn't have")
}

if !errors.Is(err, context.Canceled) {
t.Error("err is not a Context Cancelled")
}
}

0 comments on commit 2279538

Please sign in to comment.