Skip to content

Commit

Permalink
Move to no race
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Oct 7, 2021
1 parent 820b4a3 commit 282f383
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 57 deletions.
81 changes: 81 additions & 0 deletions test/norace_test.go
@@ -0,0 +1,81 @@
// Copyright 2019-2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !race

package test

import (
"context"
"crypto/rand"
"io"
"testing"
"time"

"github.com/nats-io/nats.go"
)

func TestNoRaceObjectContextOpt(t *testing.T) {
s := RunBasicJetStreamServer()
defer shutdown(s)

nc, js := jsClient(t, s)
defer nc.Close()

obs, err := js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: "OBJS"})
expectOk(t, err)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
time.AfterFunc(100*time.Millisecond, cancel)

start := time.Now()
_, err = obs.Put(&nats.ObjectMeta{Name: "TEST"}, &slow{1000}, nats.Context(ctx))
expectErr(t, err)
if delta := time.Since(start); delta > time.Second {
t.Fatalf("Cancel took too long: %v", delta)
}
si, err := js.StreamInfo("OBJ_OBJS")
expectOk(t, err)
if si.State.Msgs != 0 {
t.Fatalf("Expected no messages after canceling put, got %+v", si.State)
}

// Now put a large object in there.
blob := make([]byte, 8*1024*1024)
rand.Read(blob)
_, err = obs.PutBytes("BLOB", blob)
expectOk(t, err)

ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
time.AfterFunc(100*time.Millisecond, cancel)

time.AfterFunc(20*time.Millisecond, func() { shutdown(s) })
start = time.Now()
_, err = obs.GetBytes("BLOB", nats.Context(ctx))
expectErr(t, err)
if delta := time.Since(start); delta > time.Second {
t.Fatalf("Cancel took too long: %v", delta)
}
}

type slow struct{ n int }

func (sr *slow) Read(p []byte) (n int, err error) {
if sr.n <= 0 {
return 0, io.EOF
}
sr.n--
time.Sleep(10 * time.Millisecond)
p[0] = 'A'
return 1, nil
}
57 changes: 0 additions & 57 deletions test/object_test.go
Expand Up @@ -15,9 +15,7 @@ package test

import (
"bytes"
"context"
"crypto/rand"
"io"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -539,58 +537,3 @@ func TestObjectList(t *testing.T) {
t.Fatalf("Expected %+v but got %+v", expected, omap)
}
}

func TestObjectContextOpt(t *testing.T) {
s := RunBasicJetStreamServer()
defer shutdown(s)

nc, js := jsClient(t, s)
defer nc.Close()

obs, err := js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: "OBJS"})
expectOk(t, err)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
time.AfterFunc(100*time.Millisecond, cancel)

start := time.Now()
_, err = obs.Put(&nats.ObjectMeta{Name: "TEST"}, &slow{1000}, nats.Context(ctx))
expectErr(t, err)
if delta := time.Since(start); delta > time.Second {
t.Fatalf("Cancel took too long: %v", delta)
}
si, err := js.StreamInfo("OBJ_OBJS")
expectOk(t, err)
if si.State.Msgs != 0 {
t.Fatalf("Expected no messages after canceling put, got %+v", si.State)
}

// Now put a large object in there.
blob := make([]byte, 8*1024*1024)
rand.Read(blob)
_, err = obs.PutBytes("BLOB", blob)
expectOk(t, err)

ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
time.AfterFunc(100*time.Millisecond, cancel)

time.AfterFunc(20*time.Millisecond, func() { shutdown(s) })
start = time.Now()
_, err = obs.GetBytes("BLOB", nats.Context(ctx))
expectErr(t, err)
if delta := time.Since(start); delta > time.Second {
t.Fatalf("Cancel took too long: %v", delta)
}
}

type slow struct{ n int }

func (sr *slow) Read(p []byte) (n int, err error) {
if sr.n <= 0 {
return 0, io.EOF
}
sr.n--
time.Sleep(10 * time.Millisecond)
p[0] = 'A'
return 1, nil
}

0 comments on commit 282f383

Please sign in to comment.