Skip to content
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

code optimization #313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions blobstore/blobstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"mime/quotedprintable"
"net/http"
"net/textproto"
"os"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -89,9 +88,9 @@ var readerTest = []struct {
{"Read", 1, 0, 0, "e", nil},
{"Read", 2, 0, 0, "fg", nil},
// Test Seek.
{"Seek", 0, 2, os.SEEK_SET, "2", nil},
{"Seek", 0, 2, io.SeekStart, "2", nil},
{"Read", 5, 0, 0, "cdefg", nil},
{"Seek", 0, 2, os.SEEK_CUR, "9", nil},
{"Seek", 0, 2, io.SeekCurrent, "9", nil},
{"Read", 1, 0, 0, "j", nil},
// Test reads up to and past EOF.
{"Read", 5, 0, 0, "klmno", nil},
Expand All @@ -106,7 +105,7 @@ var readerTest = []struct {
}},
{"a14p.1", []step{
// Test Seek before any reads.
{"Seek", 0, 2, os.SEEK_SET, "2", nil},
{"Seek", 0, 2, io.SeekStart, "2", nil},
{"Read", 1, 0, 0, "c", nil},
// Test that ReadAt doesn't affect the Read offset.
{"ReadAt", 3, 9, 0, "jkl", nil},
Expand All @@ -120,11 +119,11 @@ var readerTest = []struct {
// Test basic read.
{"Read", 1, 0, 0, "A", nil},
// Test that Read returns early when the buffer is exhausted.
{"Seek", 0, rbs - 2, os.SEEK_SET, strconv.Itoa(rbs - 2), nil},
{"Seek", 0, rbs - 2, io.SeekStart, strconv.Itoa(rbs - 2), nil},
{"Read", 5, 0, 0, "AA", nil},
{"Read", 3, 0, 0, "BBB", nil},
// Test that what we just read is still in the buffer.
{"Seek", 0, rbs - 2, os.SEEK_SET, strconv.Itoa(rbs - 2), nil},
{"Seek", 0, rbs - 2, io.SeekStart, strconv.Itoa(rbs - 2), nil},
{"Read", 5, 0, 0, "AABBB", nil},
// Test ReadAt.
{"ReadAt", 3, rbs - 4, 0, "AAA", nil},
Expand All @@ -133,7 +132,7 @@ var readerTest = []struct {
{"ReadAt", 5, rbs - 4, 0, "AAAAB", nil},
{"ReadAt", 2, rbs - 4, 0, "AA", nil},
// Test seeking backwards from the Read offset.
{"Seek", 0, 2*rbs - 8, os.SEEK_SET, strconv.Itoa(2*rbs - 8), nil},
{"Seek", 0, 2*rbs - 8, io.SeekStart, strconv.Itoa(2*rbs - 8), nil},
{"Read", 1, 0, 0, "B", nil},
{"Read", 1, 0, 0, "B", nil},
{"Read", 1, 0, 0, "B", nil},
Expand Down
7 changes: 3 additions & 4 deletions blobstore/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"os"
"sync"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -113,11 +112,11 @@ func (r *reader) Seek(offset int64, whence int) (ret int64, err error) {
r.mu.Lock()
defer r.mu.Unlock()
switch whence {
case os.SEEK_SET:
case io.SeekStart:
ret = offset
case os.SEEK_CUR:
case io.SeekCurrent:
ret = r.off + int64(r.r) + offset
case os.SEEK_END:
case io.SeekEnd:
return 0, errors.New("seeking relative to the end of a blob isn't supported")
default:
return 0, fmt.Errorf("invalid Seek whence value: %d", whence)
Expand Down
2 changes: 1 addition & 1 deletion datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ var testCases = []testCase{
func checkErr(want string, err error) string {
if err != nil {
got := err.Error()
if want == "" || strings.Index(got, want) == -1 {
if want == "" || !strings.Contains(got, want) {
return got
}
} else if want != "" {
Expand Down
2 changes: 1 addition & 1 deletion datastore/prop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func TestSaveStructOmitEmpty(t *testing.T) {
actualPropNames[i] = props[i].Name
}
// Sort actuals for comparing with already sorted expected names
sort.Sort(sort.StringSlice(actualPropNames))
sort.Strings(actualPropNames)
if !reflect.DeepEqual(actualPropNames, expectedPropNames) {
t.Errorf("Expected this properties: %v, got: %v", expectedPropNames, actualPropNames)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func Call(ctx context.Context, service, method string, in, out proto.Message) er
// Default RPC timeout is 60s.
timeout := 60 * time.Second
if deadline, ok := ctx.Deadline(); ok {
timeout = deadline.Sub(time.Now())
timeout = time.Until(deadline)
}

data, err := proto.Marshal(in)
Expand Down Expand Up @@ -634,7 +634,7 @@ func (c *aeContext) logFlusher(stop <-chan int) {
tick.Stop()
return
case <-tick.C:
force := time.Now().Sub(lastFlush) > forceFlushInterval
force := time.Since(lastFlush) > forceFlushInterval
if c.flushLog(force) {
lastFlush = time.Now()
}
Expand Down