connection_go18_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // +build go1.8
  9. package mysql
  10. import (
  11. "context"
  12. "database/sql/driver"
  13. "testing"
  14. )
  15. func TestCheckNamedValue(t *testing.T) {
  16. value := driver.NamedValue{Value: ^uint64(0)}
  17. x := &mysqlConn{}
  18. err := x.CheckNamedValue(&value)
  19. if err != nil {
  20. t.Fatal("uint64 high-bit not convertible", err)
  21. }
  22. if value.Value != "18446744073709551615" {
  23. t.Fatalf("uint64 high-bit not converted, got %#v %T", value.Value, value.Value)
  24. }
  25. }
  26. // TestCleanCancel tests passed context is cancelled at start.
  27. // No packet should be sent. Connection should keep current status.
  28. func TestCleanCancel(t *testing.T) {
  29. mc := &mysqlConn{
  30. closech: make(chan struct{}),
  31. }
  32. mc.startWatcher()
  33. defer mc.cleanup()
  34. ctx, cancel := context.WithCancel(context.Background())
  35. cancel()
  36. for i := 0; i < 3; i++ { // Repeat same behavior
  37. err := mc.Ping(ctx)
  38. if err != context.Canceled {
  39. t.Errorf("expected context.Canceled, got %#v", err)
  40. }
  41. if mc.closed.IsSet() {
  42. t.Error("expected mc is not closed, closed actually")
  43. }
  44. if mc.watching {
  45. t.Error("expected watching is false, but true")
  46. }
  47. }
  48. }