本文整理汇总了Golang中google/golang.org/cloud/internal/testutil.Context函数的典型用法代码示例。如果您正苦于以下问题:Golang Context函数的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Context函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetAllWithFieldMismatch
func TestGetAllWithFieldMismatch(t *testing.T) {
type Fat struct {
X, Y int
}
type Thin struct {
X int
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
putKeys := make([]*Key, 3)
for i := range putKeys {
putKeys[i] = NewKey(c, "GetAllThing", "", int64(10+i), nil)
_, err := Put(c, putKeys[i], &Fat{X: 20 + i, Y: 30 + i})
if err != nil {
t.Fatalf("Put: %v", err)
}
}
var got []Thin
want := []Thin{
{X: 20},
{X: 21},
{X: 22},
}
getKeys, err := NewQuery("GetAllThing").GetAll(c, &got)
if len(getKeys) != 3 && !reflect.DeepEqual(getKeys, putKeys) {
t.Errorf("GetAll: keys differ\ngetKeys=%v\nputKeys=%v", getKeys, putKeys)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("GetAll: entities differ\ngot =%v\nwant=%v", got, want)
}
if _, ok := err.(*ErrFieldMismatch); !ok {
t.Errorf("GetAll: got err=%v, want ErrFieldMismatch", err)
}
}
示例2: TestProjection
func TestProjection(t *testing.T) {
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
parent := NewKey(c, "SQParent", "TestProjection", 0, nil)
now := time.Now().Truncate(time.Millisecond).Unix()
children := []*SQChild{
{I: 1 << 0, J: 100, T: now, U: now},
{I: 1 << 1, J: 100, T: now, U: now},
{I: 1 << 2, J: 200, T: now, U: now},
{I: 1 << 3, J: 300, T: now, U: now},
{I: 1 << 4, J: 300, T: now, U: now},
}
baseQuery := NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Filter("J>", 150)
testSmallQueries(t, c, parent, children, []SQTestCase{
{
"project",
baseQuery.Project("J"),
3,
200 + 300 + 300,
},
{
"distinct",
baseQuery.Project("J").Distinct(),
2,
200 + 300,
},
{
"project on meaningful (GD_WHEN) field",
baseQuery.Project("U"),
3,
0,
},
})
}
示例3: TestBasics
func TestBasics(t *testing.T) {
type X struct {
I int
S string
T time.Time
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
x0 := X{66, "99", time.Now().Truncate(time.Millisecond)}
k, err := Put(c, NewIncompleteKey(c, "BasicsX", nil), &x0)
if err != nil {
t.Fatalf("Put: %v", err)
}
x1 := X{}
err = Get(c, k, &x1)
if err != nil {
t.Errorf("Get: %v", err)
}
err = Delete(c, k)
if err != nil {
t.Errorf("Delete: %v", err)
}
if !reflect.DeepEqual(x0, x1) {
t.Errorf("compare: x0=%v, x1=%v", x0, x1)
}
}
示例4: TestACL
func TestACL(t *testing.T) {
ctx := testutil.Context(ScopeFullControl)
cleanup(t, "acl")
entity := ACLEntity("domain-google.com")
if err := PutDefaultACLRule(ctx, bucket, entity, RoleReader); err != nil {
t.Errorf("Can't put default ACL rule for the bucket, errored with %v", err)
}
for _, obj := range aclObjects {
t.Logf("Writing %v", obj)
wc := NewWriter(ctx, bucket, obj)
c := randomContents()
if _, err := wc.Write(c); err != nil {
t.Errorf("Write for %v failed with %v", obj, err)
}
if err := wc.Close(); err != nil {
t.Errorf("Close for %v failed with %v", obj, err)
}
}
name := aclObjects[0]
acl, err := ACL(ctx, bucket, name)
if err != nil {
t.Errorf("Can't retrieve ACL of %v", name)
}
aclFound := false
for _, rule := range acl {
if rule.Entity == entity && rule.Role == RoleReader {
aclFound = true
}
}
if !aclFound {
t.Error("Expected to find an ACL rule for google.com domain users, but not found")
}
if err := DeleteACLRule(ctx, bucket, name, entity); err != nil {
t.Errorf("Can't delete the ACL rule for the entity: %v", entity)
}
/* TODO(gmlewis): uncomment this section when Cloud Storage Bucket ACL APIs are working again.
if err := PutBucketACLRule(ctx, bucket, "[email protected]", RoleReader); err != nil {
t.Errorf("Error while putting bucket ACL rule: %v", err)
}
bACL, err := BucketACL(ctx, bucket)
if err != nil {
t.Errorf("Error while getting the ACL of the bucket: %v", err)
}
bACLFound := false
for _, rule := range bACL {
if rule.Entity == "[email protected]" && rule.Role == RoleReader {
bACLFound = true
}
}
if !bACLFound {
t.Error("Expected to find an ACL rule for [email protected] user, but not found")
}
if err := DeleteBucketACLRule(ctx, bucket, "[email protected]"); err != nil {
t.Errorf("Error while deleting bucket ACL rule: %v", err)
}
*/
}
示例5: TestGetMulti
func TestGetMulti(t *testing.T) {
type X struct {
I int
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
p := NewKey(c, "X", "", time.Now().Unix(), nil)
cases := []struct {
key *Key
put bool
}{
{key: NewKey(c, "X", "item1", 0, p), put: true},
{key: NewKey(c, "X", "item2", 0, p), put: false},
{key: NewKey(c, "X", "item3", 0, p), put: false},
{key: NewKey(c, "X", "item4", 0, p), put: true},
}
var src, dst []*X
var srcKeys, dstKeys []*Key
for _, c := range cases {
dst = append(dst, &X{})
dstKeys = append(dstKeys, c.key)
if c.put {
src = append(src, &X{})
srcKeys = append(srcKeys, c.key)
}
}
if _, err := PutMulti(c, srcKeys, src); err != nil {
t.Error(err)
}
err := GetMulti(c, dstKeys, dst)
if err == nil {
t.Errorf("GetMulti got %v, expected error", err)
}
e, ok := err.(MultiError)
if !ok {
t.Errorf("GetMulti got %t, expected MultiError", err)
}
for i, err := range e {
got, want := err, (error)(nil)
if !cases[i].put {
got, want = err, ErrNoSuchEntity
}
if got != want {
t.Errorf("MultiError[%d] == %v, want %v", i, got, want)
}
}
}
示例6: TestAllocateIDs
func TestAllocateIDs(t *testing.T) {
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
keys := make([]*Key, 5)
for i := range keys {
keys[i] = NewIncompleteKey(c, "AllocID", nil)
}
keys, err := AllocateIDs(c, keys)
if err != nil {
t.Errorf("AllocID #0 failed: %v", err)
}
if want := len(keys); want != 5 {
t.Errorf("Expected to allocate 5 keys, %d keys are found", want)
}
for _, k := range keys {
if k.Incomplete() {
t.Errorf("Unexpeceted incomplete key found: %v", k)
}
}
}
示例7: TestEventualConsistency
func TestEventualConsistency(t *testing.T) {
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
parent := NewKey(c, "SQParent", "TestEventualConsistency", 0, nil)
now := time.Now().Truncate(time.Millisecond).Unix()
children := []*SQChild{
{I: 0, T: now, U: now},
{I: 1, T: now, U: now},
{I: 2, T: now, U: now},
}
query := NewQuery("SQChild").Ancestor(parent).Filter("T =", now).EventualConsistency()
testSmallQueries(t, c, parent, children, nil, func() {
got, err := query.Count(c)
if err != nil {
t.Fatalf("Count: %v", err)
}
if got < 0 || 3 < got {
t.Errorf("Count: got %d, want [0,3]", got)
}
})
}
示例8: TestGetAllWithFieldMismatch
func TestGetAllWithFieldMismatch(t *testing.T) {
type Fat struct {
X, Y int
}
type Thin struct {
X int
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
// Ancestor queries (those within an entity group) are strongly consistent
// by default, which prevents a test from being flaky.
// See https://cloud.google.com/appengine/docs/go/datastore/queries#Go_Data_consistency
// for more information.
parent := NewKey(c, "SQParent", "TestGetAllWithFieldMismatch", 0, nil)
putKeys := make([]*Key, 3)
for i := range putKeys {
putKeys[i] = NewKey(c, "GetAllThing", "", int64(10+i), parent)
_, err := Put(c, putKeys[i], &Fat{X: 20 + i, Y: 30 + i})
if err != nil {
t.Fatalf("Put: %v", err)
}
}
var got []Thin
want := []Thin{
{X: 20},
{X: 21},
{X: 22},
}
getKeys, err := NewQuery("GetAllThing").Ancestor(parent).GetAll(c, &got)
if len(getKeys) != 3 && !reflect.DeepEqual(getKeys, putKeys) {
t.Errorf("GetAll: keys differ\ngetKeys=%v\nputKeys=%v", getKeys, putKeys)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("GetAll: entities differ\ngot =%v\nwant=%v", got, want)
}
if _, ok := err.(*ErrFieldMismatch); !ok {
t.Errorf("GetAll: got err=%v, want ErrFieldMismatch", err)
}
}
示例9: cleanup
func cleanup(t *testing.T, prefix string) {
ctx := testutil.Context(ScopeFullControl)
var q *Query = &Query{
Prefix: prefix,
}
for {
o, err := ListObjects(ctx, bucket, q)
if err != nil {
t.Fatalf("Cleanup List for bucket %v failed with error: %v", bucket, err)
}
for _, obj := range o.Results {
t.Logf("Cleanup deletion of %v", obj.Name)
if err = DeleteObject(ctx, bucket, obj.Name); err != nil {
t.Fatalf("Cleanup Delete for object %v failed with %v", obj.Name, err)
}
}
if o.Next == nil {
break
}
q = o.Next
}
}
示例10: TestListValues
func TestListValues(t *testing.T) {
p0 := PropertyList{
{Name: "L", Value: int64(12), Multiple: true},
{Name: "L", Value: "string", Multiple: true},
{Name: "L", Value: true, Multiple: true},
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
k, err := Put(c, NewIncompleteKey(c, "ListValue", nil), &p0)
if err != nil {
t.Fatalf("Put: %v", err)
}
var p1 PropertyList
if err := Get(c, k, &p1); err != nil {
t.Errorf("Get: %v", err)
}
if !reflect.DeepEqual(p0, p1) {
t.Errorf("compare:\np0=%v\np1=%#v", p0, p1)
}
if err = Delete(c, k); err != nil {
t.Errorf("Delete: %v", err)
}
}
示例11: TestUnindexableValues
func TestUnindexableValues(t *testing.T) {
x1500 := strings.Repeat("x", 1500)
x1501 := strings.Repeat("x", 1501)
testCases := []struct {
in Z
wantErr bool
}{
{in: Z{S: x1500}, wantErr: false},
{in: Z{S: x1501}, wantErr: true},
{in: Z{T: x1500}, wantErr: false},
{in: Z{T: x1501}, wantErr: false},
{in: Z{P: []byte(x1500)}, wantErr: false},
{in: Z{P: []byte(x1501)}, wantErr: true},
{in: Z{K: []byte(x1500)}, wantErr: false},
{in: Z{K: []byte(x1501)}, wantErr: false},
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
for _, tt := range testCases {
_, err := Put(c, NewIncompleteKey(c, "BasicsZ", nil), &tt.in)
if (err != nil) != tt.wantErr {
t.Errorf("Put %s got err %v, want err %t", tt.in, err, tt.wantErr)
}
}
}
示例12: TestAll
func TestAll(t *testing.T) {
ctx := testutil.Context(ScopePubSub, ScopeCloudPlatform)
now := time.Now()
topic := fmt.Sprintf("topic-%d", now.Unix())
subscription := fmt.Sprintf("subscription-%d", now.Unix())
if err := CreateTopic(ctx, topic); err != nil {
t.Errorf("CreateTopic error: %v", err)
}
if err := CreateSub(ctx, subscription, topic, time.Duration(0), ""); err != nil {
t.Errorf("CreateSub error: %v", err)
}
exists, err := TopicExists(ctx, topic)
if err != nil {
t.Fatalf("TopicExists error: %v", err)
}
if !exists {
t.Errorf("topic %s should exist, but it doesn't", topic)
}
exists, err = SubExists(ctx, subscription)
if err != nil {
t.Fatalf("SubExists error: %v", err)
}
if !exists {
t.Errorf("subscription %s should exist, but it doesn't", subscription)
}
max := 10
msgs := make([]*Message, max)
expectedMsgs := make(map[string]bool, max)
for i := 0; i < max; i++ {
text := fmt.Sprintf("a message with an index %d", i)
attrs := make(map[string]string)
attrs["foo"] = "bar"
msgs[i] = &Message{
Data: []byte(text),
Attributes: attrs,
}
expectedMsgs[text] = false
}
ids, err := Publish(ctx, topic, msgs...)
if err != nil {
t.Fatalf("Publish (1) error: %v", err)
}
if len(ids) != max {
t.Errorf("unexpected number of message IDs received; %d, want %d", len(ids), max)
}
expectedIDs := make(map[string]bool, max)
for _, id := range ids {
expectedIDs[id] = false
}
received, err := PullWait(ctx, subscription, max)
if err != nil {
t.Fatalf("PullWait error: %v", err)
}
if len(received) != max {
t.Errorf("unexpected number of messages received; %d, want %d", len(received), max)
}
for _, msg := range received {
expectedMsgs[string(msg.Data)] = true
expectedIDs[msg.ID] = true
if msg.Attributes["foo"] != "bar" {
t.Errorf("message attribute foo is expected to be 'bar', found '%s'", msg.Attributes["foo"])
}
}
for msg, found := range expectedMsgs {
if !found {
t.Errorf("message '%s' should be received", msg)
}
}
for id, found := range expectedIDs {
if !found {
t.Errorf("message with the message id '%s' should be received", id)
}
}
// base64 test
data := "[email protected]~"
msg := &Message{
Data: []byte(data),
}
_, err = Publish(ctx, topic, msg)
if err != nil {
t.Fatalf("Publish (2) error: %v", err)
}
received, err = PullWait(ctx, subscription, 1)
if err != nil {
t.Fatalf("PullWait error: %v", err)
//.........这里部分代码省略.........
示例13: TestAll
func TestAll(t *testing.T) {
if testing.Short() {
t.Skip("Integration tests skipped in short mode")
}
// TODO(djd): Replace this ctx with context.Background() when the new API is complete.
ctx := testutil.Context(ScopePubSub, ScopeCloudPlatform)
if ctx == nil {
t.Skip("Integration tests skipped. See CONTRIBUTING.md for details")
}
ts := testutil.TokenSource(ctx, ScopePubSub, ScopeCloudPlatform)
if ts == nil {
t.Skip("Integration tests skipped. See CONTRIBUTING.md for details")
}
now := time.Now()
topicName := fmt.Sprintf("topic-%d", now.Unix())
subName := fmt.Sprintf("subscription-%d", now.Unix())
client, err := NewClient(ctx, testutil.ProjID(), cloud.WithTokenSource(ts))
if err != nil {
t.Fatalf("Creating client error: %v", err)
}
var topic *TopicHandle
if topic, err = client.NewTopic(ctx, topicName); err != nil {
t.Errorf("CreateTopic error: %v", err)
}
var sub *SubscriptionHandle
if sub, err = topic.Subscribe(ctx, subName, nil); err != nil {
t.Errorf("CreateSub error: %v", err)
}
exists, err := topic.Exists(ctx)
if err != nil {
t.Fatalf("TopicExists error: %v", err)
}
if !exists {
t.Errorf("topic %s should exist, but it doesn't", topic)
}
exists, err = sub.Exists(ctx)
if err != nil {
t.Fatalf("SubExists error: %v", err)
}
if !exists {
t.Errorf("subscription %s should exist, but it doesn't", subName)
}
max := 10
msgs := make([]*Message, max)
expectedMsgs := make(map[string]bool, max)
for i := 0; i < max; i++ {
text := fmt.Sprintf("a message with an index %d", i)
attrs := make(map[string]string)
attrs["foo"] = "bar"
msgs[i] = &Message{
Data: []byte(text),
Attributes: attrs,
}
expectedMsgs[text] = false
}
ids, err := Publish(ctx, topicName, msgs...)
if err != nil {
t.Fatalf("Publish (1) error: %v", err)
}
if len(ids) != max {
t.Errorf("unexpected number of message IDs received; %d, want %d", len(ids), max)
}
expectedIDs := make(map[string]bool, max)
for _, id := range ids {
expectedIDs[id] = false
}
received, err := PullWait(ctx, subName, max)
if err != nil {
t.Fatalf("PullWait error: %v", err)
}
if len(received) != max {
t.Errorf("unexpected number of messages received; %d, want %d", len(received), max)
}
for _, msg := range received {
expectedMsgs[string(msg.Data)] = true
expectedIDs[msg.ID] = true
if msg.Attributes["foo"] != "bar" {
t.Errorf("message attribute foo is expected to be 'bar', found '%s'", msg.Attributes["foo"])
}
}
for msg, found := range expectedMsgs {
if !found {
t.Errorf("message '%s' should be received", msg)
}
}
//.........这里部分代码省略.........
示例14: TestObjects
func TestObjects(t *testing.T) {
ctx := testutil.Context(ScopeFullControl)
bucket = os.Getenv(envBucket)
// Cleanup.
cleanup(t, "obj")
const defaultType = "text/plain"
// Test Writer.
for _, obj := range objects {
t.Logf("Writing %v", obj)
wc := NewWriter(ctx, bucket, obj)
wc.ContentType = defaultType
c := randomContents()
if _, err := wc.Write(c); err != nil {
t.Errorf("Write for %v failed with %v", obj, err)
}
if err := wc.Close(); err != nil {
t.Errorf("Close for %v failed with %v", obj, err)
}
contents[obj] = c
}
// Test Reader.
for _, obj := range objects {
t.Logf("Creating a reader to read %v", obj)
rc, err := NewReader(ctx, bucket, obj)
if err != nil {
t.Errorf("Can't create a reader for %v, errored with %v", obj, err)
}
slurp, err := ioutil.ReadAll(rc)
if err != nil {
t.Errorf("Can't ReadAll object %v, errored with %v", obj, err)
}
if got, want := slurp, contents[obj]; !bytes.Equal(got, want) {
t.Errorf("Contents (%v) = %q; want %q", obj, got, want)
}
rc.Close()
}
// Test NotFound.
_, err := NewReader(ctx, bucket, "obj-not-exists")
if err != ErrObjectNotExist {
t.Errorf("Object should not exist, err found to be %v", err)
}
name := objects[0]
// Test StatObject.
o, err := StatObject(ctx, bucket, name)
if err != nil {
t.Error(err)
}
if got, want := o.Name, name; got != want {
t.Errorf("Name (%v) = %q; want %q", name, got, want)
}
if got, want := o.ContentType, defaultType; got != want {
t.Errorf("ContentType (%v) = %q; want %q", name, got, want)
}
// Test object copy.
copy, err := CopyObject(ctx, bucket, name, bucket, copyObj, nil)
if err != nil {
t.Errorf("CopyObject failed with %v", err)
}
if copy.Name != copyObj {
t.Errorf("Copy object's name = %q; want %q", copy.Name, copyObj)
}
if copy.Bucket != bucket {
t.Errorf("Copy object's bucket = %q; want %q", copy.Bucket, bucket)
}
// Test UpdateAttrs.
updated, err := UpdateAttrs(ctx, bucket, name, ObjectAttrs{
ContentType: "text/html",
ACL: []ACLRule{{Entity: "domain-google.com", Role: RoleReader}},
})
if err != nil {
t.Errorf("UpdateAttrs failed with %v", err)
}
if want := "text/html"; updated.ContentType != want {
t.Errorf("updated.ContentType == %q; want %q", updated.ContentType, want)
}
// Test checksums.
checksumCases := []struct {
name string
contents [][]byte
size int64
md5 string
crc32c uint32
}{
{
name: "checksum-object",
contents: [][]byte{[]byte("hello"), []byte("world")},
size: 10,
md5: "fc5e038d38a57032085441e7fe7010b0",
crc32c: 1456190592,
},
//.........这里部分代码省略.........
示例15: TestKindlessQueries
func TestKindlessQueries(t *testing.T) {
type Dee struct {
I int
Why string
}
type Dum struct {
I int
Pling string
}
c := testutil.Context(ScopeDatastore, ScopeUserEmail)
parent := NewKey(c, "Tweedle", "tweedle", 0, nil)
keys := []*Key{
NewKey(c, "Dee", "dee0", 0, parent),
NewKey(c, "Dum", "dum1", 0, parent),
NewKey(c, "Dum", "dum2", 0, parent),
NewKey(c, "Dum", "dum3", 0, parent),
}
src := []interface{}{
&Dee{1, "binary0001"},
&Dum{2, "binary0010"},
&Dum{4, "binary0100"},
&Dum{8, "binary1000"},
}
keys, err := PutMulti(c, keys, src)
if err != nil {
t.Fatalf("put: %v", err)
}
testCases := []struct {
desc string
query *Query
want []int
wantErr string
}{
{
desc: "Dee",
query: NewQuery("Dee"),
want: []int{1},
},
{
desc: "Doh",
query: NewQuery("Doh"),
want: nil},
{
desc: "Dum",
query: NewQuery("Dum"),
want: []int{2, 4, 8},
},
{
desc: "",
query: NewQuery(""),
want: []int{1, 2, 4, 8},
},
{
desc: "Kindless filter",
query: NewQuery("").Filter("__key__ =", keys[2]),
want: []int{4},
},
{
desc: "Kindless order",
query: NewQuery("").Order("__key__"),
want: []int{1, 2, 4, 8},
},
{
desc: "Kindless bad filter",
query: NewQuery("").Filter("I =", 4),
wantErr: "kind is required for filter: I",
},
{
desc: "Kindless bad order",
query: NewQuery("").Order("-__key__"),
wantErr: "kind is required for all orders except __key__ ascending",
},
}
loop:
for _, tc := range testCases {
q := tc.query.Ancestor(parent)
gotCount, err := q.Count(c)
if err != nil {
if tc.wantErr == "" || !strings.Contains(err.Error(), tc.wantErr) {
t.Errorf("count %q: err %v, want err %q", tc.desc, err, tc.wantErr)
}
continue
}
if tc.wantErr != "" {
t.Errorf("count %q: want err %q", tc.desc, tc.wantErr)
continue
}
if gotCount != len(tc.want) {
t.Errorf("count %q: got %d want %d", tc.desc, gotCount, len(tc.want))
continue
}
var got []int
for iter := q.Run(c); ; {
var dst struct {
I int
Why, Pling string
}
//.........这里部分代码省略.........