Skip to content

Commit

Permalink
Merge pull request #963 from go-kivik/bug929
Browse files Browse the repository at this point in the history
Fix bug 929
  • Loading branch information
flimzy committed May 10, 2024
2 parents 3e5b6d6 + 339ffcf commit a1f00dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
28 changes: 27 additions & 1 deletion couchdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (p *updatesParser) parseMeta(i interface{}, dec *json.Decoder, key string)
return meta.parseMeta(key, dec)
}

func newUpdates(ctx context.Context, r io.ReadCloser) (*couchUpdates, error) {
func newUpdates(ctx context.Context, r io.ReadCloser) (driver.DBUpdates, error) {
r, feedType, err := updatesFeedType(r)
if err != nil {
return nil, &internal.Error{Status: http.StatusBadGateway, Err: err}
Expand All @@ -136,6 +136,8 @@ func newUpdates(ctx context.Context, r io.ReadCloser) (*couchUpdates, error) {
return newContinuousUpdates(ctx, r), nil
case feedTypeNormal:
return newNormalUpdates(ctx, r), nil
case feedTypeEmpty:
return newEmptyUpdates(r)
}
panic("unknown") // TODO: test
}
Expand All @@ -145,6 +147,7 @@ type feedType int
const (
feedTypeNormal feedType = iota
feedTypeContinuous
feedTypeEmpty
)

// updatesFeedType detects the type of Updates Feed (continuous, or normal) by
Expand Down Expand Up @@ -183,6 +186,8 @@ func updatesFeedType(r io.ReadCloser) (io.ReadCloser, feedType, error) {
return r, feedTypeContinuous, nil
case "results": // Normal feed
return r, feedTypeNormal, nil
case "last_seq": // Normal feed, but with no results
return r, feedTypeEmpty, nil
default: // Something unexpected
return nil, 0, fmt.Errorf("kivik: unexpected JSON token %q in feed response, expected `db_name` or `results`", tok)
}
Expand All @@ -202,6 +207,27 @@ func newContinuousUpdates(ctx context.Context, r io.ReadCloser) *couchUpdates {
}
}

func newEmptyUpdates(r io.ReadCloser) (driver.DBUpdates, error) {
var updates emptyUpdates
if err := json.NewDecoder(r).Decode(&updates); err != nil {
return nil, err
}
return &updates, nil
}

type emptyUpdates struct {
Lastseq string `json:"last_seq"`
}

var (
_ driver.DBUpdates = (*emptyUpdates)(nil)
_ driver.LastSeqer = (*emptyUpdates)(nil)
)

func (u *emptyUpdates) Close() error { return nil }
func (u *emptyUpdates) Next(*driver.DBUpdate) error { return io.EOF }
func (u *emptyUpdates) LastSeq() (string, error) { return u.Lastseq, nil }

func (u *couchUpdates) Next(update *driver.DBUpdate) error {
return u.iter.next(update)
}
Expand Down
19 changes: 14 additions & 5 deletions couchdb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ func TestDBUpdates(t *testing.T) {
wantStatus: http.StatusBadRequest,
wantErr: "eventsource feed type not supported",
},
{
// Based on CI test failures, presumably from a race condition that
// causes the query to happen before any database is created.
name: "no databases",
client: newTestClient(&http.Response{
StatusCode: 200,
Header: http.Header{
"Content-Type": {"application/json"},
},
Body: Body(`{"last_seq":"38-g1AAAACLeJzLYWBgYMpgTmHgzcvPy09JdcjLz8gvLskBCScyJNX___8_K4M5UTgXKMBuZmFmYWFgjq4Yh_Y8FiDJ0ACk_qOYYpyanGiQYoquJwsAM_UqgA"}`),
}, nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -397,9 +409,6 @@ func TestDBUpdates(t *testing.T) {
if err != nil {
return
}
if _, ok := result.(*couchUpdates); !ok {
t.Errorf("Unexpected type returned: %t", result)
}

var got []driver.DBUpdate
for {
Expand All @@ -420,7 +429,7 @@ func TestDBUpdates(t *testing.T) {
}
}

func newTestUpdates(t *testing.T, body io.ReadCloser) *couchUpdates {
func newTestUpdates(t *testing.T, body io.ReadCloser) driver.DBUpdates {
t.Helper()
u, err := newUpdates(context.Background(), body)
if err != nil {
Expand All @@ -433,7 +442,7 @@ func TestUpdatesNext(t *testing.T) {
t.Parallel()
tests := []struct {
name string
updates *couchUpdates
updates driver.DBUpdates
status int
err string
expected *driver.DBUpdate
Expand Down

0 comments on commit a1f00dc

Please sign in to comment.