You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

207 lines
5.9 KiB

  1. /*
  2. Copyright 2017 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spanner
  14. import (
  15. "testing"
  16. "time"
  17. pbd "github.com/golang/protobuf/ptypes/duration"
  18. pbt "github.com/golang/protobuf/ptypes/timestamp"
  19. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  20. )
  21. // Test generating TimestampBound for strong reads.
  22. func TestStrong(t *testing.T) {
  23. got := StrongRead()
  24. want := TimestampBound{mode: strong}
  25. if !testEqual(got, want) {
  26. t.Errorf("Strong() = %v; want %v", got, want)
  27. }
  28. }
  29. // Test generating TimestampBound for reads with exact staleness.
  30. func TestExactStaleness(t *testing.T) {
  31. got := ExactStaleness(10 * time.Second)
  32. want := TimestampBound{mode: exactStaleness, d: 10 * time.Second}
  33. if !testEqual(got, want) {
  34. t.Errorf("ExactStaleness(10*time.Second) = %v; want %v", got, want)
  35. }
  36. }
  37. // Test generating TimestampBound for reads with max staleness.
  38. func TestMaxStaleness(t *testing.T) {
  39. got := MaxStaleness(10 * time.Second)
  40. want := TimestampBound{mode: maxStaleness, d: 10 * time.Second}
  41. if !testEqual(got, want) {
  42. t.Errorf("MaxStaleness(10*time.Second) = %v; want %v", got, want)
  43. }
  44. }
  45. // Test generating TimestampBound for reads with minimum freshness requirement.
  46. func TestMinReadTimestamp(t *testing.T) {
  47. ts := time.Now()
  48. got := MinReadTimestamp(ts)
  49. want := TimestampBound{mode: minReadTimestamp, t: ts}
  50. if !testEqual(got, want) {
  51. t.Errorf("MinReadTimestamp(%v) = %v; want %v", ts, got, want)
  52. }
  53. }
  54. // Test generating TimestampBound for reads requesting data at a exact timestamp.
  55. func TestReadTimestamp(t *testing.T) {
  56. ts := time.Now()
  57. got := ReadTimestamp(ts)
  58. want := TimestampBound{mode: readTimestamp, t: ts}
  59. if !testEqual(got, want) {
  60. t.Errorf("ReadTimestamp(%v) = %v; want %v", ts, got, want)
  61. }
  62. }
  63. // Test TimestampBound.String.
  64. func TestTimestampBoundString(t *testing.T) {
  65. ts := time.Unix(1136239445, 0).UTC()
  66. var tests = []struct {
  67. tb TimestampBound
  68. want string
  69. }{
  70. {
  71. tb: TimestampBound{mode: strong},
  72. want: "(strong)",
  73. },
  74. {
  75. tb: TimestampBound{mode: exactStaleness, d: 10 * time.Second},
  76. want: "(exactStaleness: 10s)",
  77. },
  78. {
  79. tb: TimestampBound{mode: maxStaleness, d: 10 * time.Second},
  80. want: "(maxStaleness: 10s)",
  81. },
  82. {
  83. tb: TimestampBound{mode: minReadTimestamp, t: ts},
  84. want: "(minReadTimestamp: 2006-01-02 22:04:05 +0000 UTC)",
  85. },
  86. {
  87. tb: TimestampBound{mode: readTimestamp, t: ts},
  88. want: "(readTimestamp: 2006-01-02 22:04:05 +0000 UTC)",
  89. },
  90. }
  91. for _, test := range tests {
  92. got := test.tb.String()
  93. if got != test.want {
  94. t.Errorf("%#v.String():\ngot %q\nwant %q", test.tb, got, test.want)
  95. }
  96. }
  97. }
  98. // Test time.Duration to pdb.Duration conversion.
  99. func TestDurationProto(t *testing.T) {
  100. var tests = []struct {
  101. d time.Duration
  102. want pbd.Duration
  103. }{
  104. {time.Duration(0), pbd.Duration{Seconds: 0, Nanos: 0}},
  105. {time.Second, pbd.Duration{Seconds: 1, Nanos: 0}},
  106. {time.Millisecond, pbd.Duration{Seconds: 0, Nanos: 1e6}},
  107. {15 * time.Nanosecond, pbd.Duration{Seconds: 0, Nanos: 15}},
  108. {42 * time.Hour, pbd.Duration{Seconds: 151200}},
  109. {-(1*time.Hour + 4*time.Millisecond), pbd.Duration{Seconds: -3600, Nanos: -4e6}},
  110. }
  111. for _, test := range tests {
  112. got := durationProto(test.d)
  113. if !testEqual(got, &test.want) {
  114. t.Errorf("durationProto(%v) = %v; want %v", test.d, got, test.want)
  115. }
  116. }
  117. }
  118. // Test time.Time to pbt.Timestamp conversion.
  119. func TestTimeProto(t *testing.T) {
  120. var tests = []struct {
  121. t time.Time
  122. want pbt.Timestamp
  123. }{
  124. {time.Unix(0, 0), pbt.Timestamp{}},
  125. {time.Unix(1136239445, 12345), pbt.Timestamp{Seconds: 1136239445, Nanos: 12345}},
  126. {time.Unix(-1000, 12345), pbt.Timestamp{Seconds: -1000, Nanos: 12345}},
  127. }
  128. for _, test := range tests {
  129. got := timestampProto(test.t)
  130. if !testEqual(got, &test.want) {
  131. t.Errorf("timestampProto(%v) = %v; want %v", test.t, got, test.want)
  132. }
  133. }
  134. }
  135. // Test readonly transaction option builder.
  136. func TestBuildTransactionOptionsReadOnly(t *testing.T) {
  137. ts := time.Unix(1136239445, 12345)
  138. var tests = []struct {
  139. tb TimestampBound
  140. ts bool
  141. want sppb.TransactionOptions_ReadOnly
  142. }{
  143. {
  144. StrongRead(), false,
  145. sppb.TransactionOptions_ReadOnly{
  146. TimestampBound: &sppb.TransactionOptions_ReadOnly_Strong{
  147. Strong: true},
  148. ReturnReadTimestamp: false,
  149. },
  150. },
  151. {
  152. ExactStaleness(10 * time.Second), true,
  153. sppb.TransactionOptions_ReadOnly{
  154. TimestampBound: &sppb.TransactionOptions_ReadOnly_ExactStaleness{
  155. ExactStaleness: &pbd.Duration{Seconds: 10}},
  156. ReturnReadTimestamp: true,
  157. },
  158. },
  159. {
  160. MaxStaleness(10 * time.Second), true,
  161. sppb.TransactionOptions_ReadOnly{
  162. TimestampBound: &sppb.TransactionOptions_ReadOnly_MaxStaleness{
  163. MaxStaleness: &pbd.Duration{Seconds: 10}},
  164. ReturnReadTimestamp: true,
  165. },
  166. },
  167. {
  168. MinReadTimestamp(ts), true,
  169. sppb.TransactionOptions_ReadOnly{
  170. TimestampBound: &sppb.TransactionOptions_ReadOnly_MinReadTimestamp{
  171. MinReadTimestamp: &pbt.Timestamp{Seconds: 1136239445, Nanos: 12345}},
  172. ReturnReadTimestamp: true,
  173. },
  174. },
  175. {
  176. ReadTimestamp(ts), true,
  177. sppb.TransactionOptions_ReadOnly{
  178. TimestampBound: &sppb.TransactionOptions_ReadOnly_ReadTimestamp{
  179. ReadTimestamp: &pbt.Timestamp{Seconds: 1136239445, Nanos: 12345}},
  180. ReturnReadTimestamp: true,
  181. },
  182. },
  183. }
  184. for _, test := range tests {
  185. got := buildTransactionOptionsReadOnly(test.tb, test.ts)
  186. if !testEqual(got, &test.want) {
  187. t.Errorf("buildTransactionOptionsReadOnly(%v,%v) = %v; want %v", test.tb, test.ts, got, test.want)
  188. }
  189. }
  190. }