atomic_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package atomic
  9. import (
  10. "errors"
  11. "testing"
  12. )
  13. var (
  14. errOne = errors.New("one")
  15. errTwo = errors.New("two")
  16. )
  17. func TestAtomicBool(t *testing.T) {
  18. var b Bool
  19. if b.IsSet() {
  20. t.Fatal("Expected value to be false")
  21. }
  22. b.Set(true)
  23. if b.value != 1 {
  24. t.Fatal("Set(true) did not set value to 1")
  25. }
  26. if !b.IsSet() {
  27. t.Fatal("Expected value to be true")
  28. }
  29. b.Set(true)
  30. if !b.IsSet() {
  31. t.Fatal("Expected value to be true")
  32. }
  33. b.Set(false)
  34. if b.value != 0 {
  35. t.Fatal("Set(false) did not set value to 0")
  36. }
  37. if b.IsSet() {
  38. t.Fatal("Expected value to be false")
  39. }
  40. b.Set(false)
  41. if b.IsSet() {
  42. t.Fatal("Expected value to be false")
  43. }
  44. if b.TrySet(false) {
  45. t.Fatal("Expected TrySet(false) to fail")
  46. }
  47. if !b.TrySet(true) {
  48. t.Fatal("Expected TrySet(true) to succeed")
  49. }
  50. if !b.IsSet() {
  51. t.Fatal("Expected value to be true")
  52. }
  53. b.Set(true)
  54. if !b.IsSet() {
  55. t.Fatal("Expected value to be true")
  56. }
  57. if b.TrySet(true) {
  58. t.Fatal("Expected TrySet(true) to fail")
  59. }
  60. if !b.TrySet(false) {
  61. t.Fatal("Expected TrySet(false) to succeed")
  62. }
  63. if b.IsSet() {
  64. t.Fatal("Expected value to be false")
  65. }
  66. b._noCopy.Lock() // we've "tested" it ¯\_(ツ)_/¯
  67. }
  68. func TestAtomicError(t *testing.T) {
  69. var e Error
  70. if e.Value() != nil {
  71. t.Fatal("Expected value to be nil")
  72. }
  73. e.Set(errOne)
  74. if v := e.Value(); v != errOne {
  75. if v == nil {
  76. t.Fatal("Value is still nil")
  77. }
  78. t.Fatal("Error did not match")
  79. }
  80. e.Set(errTwo)
  81. if e.Value() == errOne {
  82. t.Fatal("Error still matches old error")
  83. }
  84. if v := e.Value(); v != errTwo {
  85. t.Fatal("Error did not match")
  86. }
  87. }