routing_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. require_relative 'spec_helper'
  3. describe "Routing" do
  4. let(:processes) { Helpers::Pgcat.single_shard_setup("sharded_db", 5) }
  5. after do
  6. processes.all_databases.map(&:reset)
  7. processes.pgcat.shutdown
  8. end
  9. describe "SET ROLE" do
  10. context "primary" do
  11. it "routes queries only to primary" do
  12. conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
  13. conn.async_exec("SET SERVER ROLE to 'primary'")
  14. query_count = 30
  15. failed_count = 0
  16. query_count.times do
  17. conn.async_exec("SELECT 1 + 2")
  18. rescue
  19. failed_count += 1
  20. end
  21. expect(failed_count).to eq(0)
  22. processes.replicas.map(&:count_select_1_plus_2).each do |instance_share|
  23. expect(instance_share).to eq(0)
  24. end
  25. expect(processes.primary.count_select_1_plus_2).to eq(query_count)
  26. end
  27. end
  28. context "replica" do
  29. it "routes queries only to replicas" do
  30. conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
  31. conn.async_exec("SET SERVER ROLE to 'replica'")
  32. expected_share = QUERY_COUNT / processes.replicas.count
  33. failed_count = 0
  34. QUERY_COUNT.times do
  35. conn.async_exec("SELECT 1 + 2")
  36. rescue
  37. failed_count += 1
  38. end
  39. expect(failed_count).to eq(0)
  40. processes.replicas.map(&:count_select_1_plus_2).each do |instance_share|
  41. expect(instance_share).to be_within(expected_share * MARGIN_OF_ERROR).of(expected_share)
  42. end
  43. expect(processes.primary.count_select_1_plus_2).to eq(0)
  44. end
  45. end
  46. context "any" do
  47. it "routes queries to all instances" do
  48. conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
  49. conn.async_exec("SET SERVER ROLE to 'any'")
  50. expected_share = QUERY_COUNT / processes.all_databases.count
  51. failed_count = 0
  52. QUERY_COUNT.times do
  53. conn.async_exec("SELECT 1 + 2")
  54. rescue
  55. failed_count += 1
  56. end
  57. expect(failed_count).to eq(0)
  58. processes.all_databases.map(&:count_select_1_plus_2).each do |instance_share|
  59. expect(instance_share).to be_within(expected_share * MARGIN_OF_ERROR).of(expected_share)
  60. end
  61. end
  62. end
  63. end
  64. end