topology_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) 2012 The gocql 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. package gocql
  5. import (
  6. "testing"
  7. )
  8. // fakeNode is used as a simple structure to test the RoundRobin API
  9. type fakeNode struct {
  10. conn *Conn
  11. closed bool
  12. }
  13. // Pick is needed to satisfy the Node interface
  14. func (n *fakeNode) Pick(qry *Query) *Conn {
  15. if n.conn == nil {
  16. n.conn = &Conn{}
  17. }
  18. return n.conn
  19. }
  20. //Close is needed to satisfy the Node interface
  21. func (n *fakeNode) Close() {
  22. n.closed = true
  23. }
  24. //TestRoundRobinAPI tests the exported methods of the RoundRobin struct
  25. //to make sure the API behaves accordingly.
  26. func TestRoundRobinAPI(t *testing.T) {
  27. node := &fakeNode{}
  28. rr := NewRoundRobin()
  29. rr.AddNode(node)
  30. if rr.Size() != 1 {
  31. t.Fatalf("expected size to be 1, got %v", rr.Size())
  32. }
  33. if c := rr.Pick(nil); c != node.conn {
  34. t.Fatalf("expected conn %v, got %v", node.conn, c)
  35. }
  36. rr.Close()
  37. if rr.pool != nil {
  38. t.Fatalf("expected rr.pool to be nil, got %v", rr.pool)
  39. }
  40. if !node.closed {
  41. t.Fatal("expected node.closed to be true, got false")
  42. }
  43. }