Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package vsync |
| 6 | |
| 7 | // Utilities for testing sync. |
| 8 | |
| 9 | import ( |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 10 | "fmt" |
| 11 | "os" |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 12 | "path" |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 13 | "testing" |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 14 | "time" |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 15 | |
Jatin Lodhia | af93faa | 2015-07-17 15:57:36 -0700 | [diff] [blame] | 16 | wire "v.io/syncbase/v23/services/syncbase/nosql" |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 17 | "v.io/syncbase/x/ref/services/syncbase/server/interfaces" |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 18 | "v.io/syncbase/x/ref/services/syncbase/server/util" |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 19 | "v.io/syncbase/x/ref/services/syncbase/server/watchable" |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 20 | "v.io/syncbase/x/ref/services/syncbase/store" |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 21 | "v.io/v23/context" |
| 22 | "v.io/v23/rpc" |
| 23 | "v.io/v23/security/access" |
| 24 | "v.io/v23/verror" |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 25 | "v.io/x/ref/test" |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // mockService emulates a Syncbase service that includes store and sync. |
| 29 | // It is used to access a mock application. |
| 30 | type mockService struct { |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 31 | engine string |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 32 | dir string |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 33 | st store.Store |
| 34 | sync *syncService |
| 35 | shutdown func() |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (s *mockService) St() store.Store { |
| 39 | return s.st |
| 40 | } |
| 41 | |
Himabindu Pucha | 41212cb | 2015-06-02 11:34:05 -0700 | [diff] [blame] | 42 | func (s *mockService) Sync() interfaces.SyncServerMethods { |
| 43 | return s.sync |
| 44 | } |
| 45 | |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 46 | func (s *mockService) App(ctx *context.T, call rpc.ServerCall, appName string) (interfaces.App, error) { |
Himabindu Pucha | c124703 | 2015-08-13 13:42:01 -0700 | [diff] [blame] | 47 | return &mockApp{st: s.st}, nil |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func (s *mockService) AppNames(ctx *context.T, call rpc.ServerCall) ([]string, error) { |
| 51 | return []string{"mockapp"}, nil |
| 52 | } |
| 53 | |
| 54 | // mockApp emulates a Syncbase App. It is used to access a mock database. |
| 55 | type mockApp struct { |
Himabindu Pucha | c124703 | 2015-08-13 13:42:01 -0700 | [diff] [blame] | 56 | st store.Store |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func (a *mockApp) NoSQLDatabase(ctx *context.T, call rpc.ServerCall, dbName string) (interfaces.Database, error) { |
Himabindu Pucha | c124703 | 2015-08-13 13:42:01 -0700 | [diff] [blame] | 60 | return &mockDatabase{st: a.st}, nil |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (a *mockApp) NoSQLDatabaseNames(ctx *context.T, call rpc.ServerCall) ([]string, error) { |
| 64 | return []string{"mockdb"}, nil |
| 65 | } |
| 66 | |
Jatin Lodhia | af93faa | 2015-07-17 15:57:36 -0700 | [diff] [blame] | 67 | func (a *mockApp) CreateNoSQLDatabase(ctx *context.T, call rpc.ServerCall, dbName string, perms access.Permissions, metadata *wire.SchemaMetadata) error { |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 68 | return verror.NewErrNotImplemented(ctx) |
| 69 | } |
| 70 | |
| 71 | func (a *mockApp) DeleteNoSQLDatabase(ctx *context.T, call rpc.ServerCall, dbName string) error { |
| 72 | return verror.NewErrNotImplemented(ctx) |
| 73 | } |
| 74 | |
| 75 | func (a *mockApp) SetDatabasePerms(ctx *context.T, call rpc.ServerCall, dbName string, perms access.Permissions, version string) error { |
| 76 | return verror.NewErrNotImplemented(ctx) |
| 77 | } |
| 78 | |
Himabindu Pucha | 41212cb | 2015-06-02 11:34:05 -0700 | [diff] [blame] | 79 | func (a *mockApp) Service() interfaces.Service { |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func (a *mockApp) Name() string { |
| 84 | return "mockapp" |
| 85 | } |
| 86 | |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 87 | // mockDatabase emulates a Syncbase Database. It is used to test sync functionality. |
| 88 | type mockDatabase struct { |
Himabindu Pucha | c124703 | 2015-08-13 13:42:01 -0700 | [diff] [blame] | 89 | st store.Store |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func (d *mockDatabase) St() store.Store { |
| 93 | return d.st |
| 94 | } |
| 95 | |
Himabindu Pucha | b480ea8 | 2015-07-21 20:46:33 -0700 | [diff] [blame] | 96 | func (d *mockDatabase) CheckPermsInternal(ctx *context.T, call rpc.ServerCall, st store.StoreReader) error { |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 97 | return verror.NewErrNotImplemented(ctx) |
| 98 | } |
| 99 | |
| 100 | func (d *mockDatabase) SetPermsInternal(ctx *context.T, call rpc.ServerCall, perms access.Permissions, version string) error { |
| 101 | return verror.NewErrNotImplemented(ctx) |
| 102 | } |
| 103 | |
Himabindu Pucha | 41212cb | 2015-06-02 11:34:05 -0700 | [diff] [blame] | 104 | func (d *mockDatabase) Name() string { |
| 105 | return "mockdb" |
| 106 | } |
| 107 | |
Himabindu Pucha | 41212cb | 2015-06-02 11:34:05 -0700 | [diff] [blame] | 108 | func (d *mockDatabase) App() interfaces.App { |
| 109 | return nil |
| 110 | } |
| 111 | |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 112 | // createService creates a mock Syncbase service used for testing sync functionality. |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 113 | func createService(t *testing.T) *mockService { |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 114 | ctx, shutdown := test.V23Init() |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 115 | engine := "leveldb" |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 116 | opts := util.OpenOptions{CreateIfMissing: true, ErrorIfExists: false} |
| 117 | dir := fmt.Sprintf("%s/vsync_test_%d_%d", os.TempDir(), os.Getpid(), time.Now().UnixNano()) |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 118 | |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 119 | st, err := util.OpenStore(engine, path.Join(dir, engine), opts) |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 120 | if err != nil { |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 121 | t.Fatalf("cannot create store %s (%s): %v", engine, dir, err) |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 122 | } |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 123 | st, err = watchable.Wrap(st, &watchable.Options{ |
Raja Daoud | b2837ea | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 124 | ManagedPrefixes: []string{util.RowPrefix, util.PermsPrefix}, |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 125 | }) |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 126 | |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 127 | s := &mockService{ |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 128 | st: st, |
| 129 | engine: engine, |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 130 | dir: dir, |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 131 | shutdown: shutdown, |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 132 | } |
Himabindu Pucha | c124703 | 2015-08-13 13:42:01 -0700 | [diff] [blame] | 133 | if s.sync, err = New(ctx, nil, s, nil, dir); err != nil { |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 134 | util.DestroyStore(engine, dir) |
Raja Daoud | 1471090 | 2015-06-01 18:06:02 -0700 | [diff] [blame] | 135 | t.Fatalf("cannot create sync service: %v", err) |
| 136 | } |
| 137 | return s |
| 138 | } |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 139 | |
| 140 | // destroyService cleans up the mock Syncbase service. |
| 141 | func destroyService(t *testing.T, s *mockService) { |
Himabindu Pucha | 19133a0 | 2015-06-26 17:56:09 -0700 | [diff] [blame] | 142 | defer s.shutdown() |
| 143 | defer s.sync.Close() |
Raja Daoud | d6a7557 | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 144 | if err := util.DestroyStore(s.engine, s.dir); err != nil { |
| 145 | t.Fatalf("cannot destroy store %s (%s): %v", s.engine, s.dir, err) |
Raja Daoud | fe2239f | 2015-06-17 17:48:01 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
Raja Daoud | b2837ea | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 148 | |
Raja Daoud | b2837ea | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 149 | // makeRowKey returns the database row key for a given application key. |
| 150 | func makeRowKey(key string) string { |
| 151 | return util.JoinKeyParts(util.RowPrefix, key) |
| 152 | } |