topology_test.go 999 B

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