|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "timecop" |
| 4 | + |
| 5 | +RSpec.describe GraphQL::AnyCable::Cleaner do |
| 6 | + let(:query) do |
| 7 | + <<~GRAPHQL |
| 8 | + subscription SomeSubscription { productUpdated { id } } |
| 9 | + GRAPHQL |
| 10 | + end |
| 11 | + |
| 12 | + let(:channel) do |
| 13 | + socket = double("Socket", istate: AnyCable::Socket::State.new({})) |
| 14 | + connection = double("Connection", anycable_socket: socket) |
| 15 | + double("Channel", id: "legacy_id", params: { "channelId" => "legacy_id" }, stream_from: nil, connection: connection) |
| 16 | + end |
| 17 | + |
| 18 | + let(:subscription_id) do |
| 19 | + "some-truly-random-number" |
| 20 | + end |
| 21 | + |
| 22 | + let(:redis) { GraphQL::AnyCable.redis } |
| 23 | + |
| 24 | + before do |
| 25 | + AnycableSchema.execute( |
| 26 | + query: query, |
| 27 | + context: { channel: channel, subscription_id: subscription_id }, |
| 28 | + variables: {}, |
| 29 | + operation_name: "SomeSubscription", |
| 30 | + ) |
| 31 | + end |
| 32 | + |
| 33 | + describe ".clean_subscriptions" do |
| 34 | + context "when expired_seconds passed via argument" do |
| 35 | + context "when subscriptions are expired" do |
| 36 | + let(:lifetime_in_seconds) { 10 } |
| 37 | + |
| 38 | + it "cleans subscriptions" do |
| 39 | + expect(redis.keys("graphql-subscription:*").count).to be > 0 |
| 40 | + |
| 41 | + Timecop.freeze(Time.now + 10) do |
| 42 | + described_class.clean_subscriptions(lifetime_in_seconds) |
| 43 | + end |
| 44 | + |
| 45 | + expect(redis.keys("graphql-subscription:*").count).to be 0 |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "when subscriptions are not expired" do |
| 50 | + let(:lifetime_in_seconds) { 100 } |
| 51 | + |
| 52 | + it "not cleans subscriptions" do |
| 53 | + described_class.clean_subscriptions(lifetime_in_seconds) |
| 54 | + |
| 55 | + expect(redis.keys("graphql-subscription:*").count).to be > 0 |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context "when expired_seconds passed via config" do |
| 61 | + context "when subscriptions are expired" do |
| 62 | + around do |ex| |
| 63 | + old_value = GraphQL::AnyCable.config.subscription_expiration_seconds |
| 64 | + GraphQL::AnyCable.config.subscription_expiration_seconds = 10 |
| 65 | + |
| 66 | + ex.run |
| 67 | + |
| 68 | + GraphQL::AnyCable.config.subscription_expiration_seconds = old_value |
| 69 | + end |
| 70 | + |
| 71 | + it "cleans subscriptions" do |
| 72 | + expect(redis.keys("graphql-subscription:*").count).to be > 0 |
| 73 | + |
| 74 | + Timecop.freeze(Time.now + 10) do |
| 75 | + described_class.clean_subscriptions |
| 76 | + end |
| 77 | + |
| 78 | + expect(redis.keys("graphql-subscription:*").count).to be 0 |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + context "when config.subscription_expiration_seconds is nil" do |
| 83 | + it "remains subscriptions" do |
| 84 | + Timecop.freeze(Time.now + 10) do |
| 85 | + described_class.clean_subscriptions |
| 86 | + end |
| 87 | + |
| 88 | + expect(redis.keys("graphql-subscription:*").count).to be > 0 |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context "when an expiration_seconds is not positive integer" do |
| 94 | + it "does not clean subscriptions" do |
| 95 | + expect(described_class).to_not receive(:remove_old_objects) |
| 96 | + |
| 97 | + described_class.clean_subscriptions("") |
| 98 | + |
| 99 | + expect(redis.keys("graphql-subscription:*").count).to be > 0 |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + describe ".clean_channels" do |
| 105 | + context "when expired_seconds passed via argument" do |
| 106 | + context "when channels are expired" do |
| 107 | + let(:lifetime_in_seconds) { 10 } |
| 108 | + |
| 109 | + it "cleans subscriptions" do |
| 110 | + expect(redis.keys("graphql-channel:*").count).to be > 0 |
| 111 | + |
| 112 | + Timecop.freeze(Time.now + 10) do |
| 113 | + described_class.clean_channels(lifetime_in_seconds) |
| 114 | + end |
| 115 | + |
| 116 | + expect(redis.keys("graphql-channel:*").count).to be 0 |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context "when channels are not expired" do |
| 121 | + let(:lifetime_in_seconds) { 100 } |
| 122 | + |
| 123 | + it "does not clean channels" do |
| 124 | + described_class.clean_channels(lifetime_in_seconds) |
| 125 | + |
| 126 | + expect(redis.keys("graphql-channel:*").count).to be > 0 |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + context "when an expiration_seconds is not positive integer" do |
| 132 | + it "does not clean channels" do |
| 133 | + expect(described_class).to_not receive(:remove_old_objects) |
| 134 | + |
| 135 | + described_class.clean_channels("") |
| 136 | + |
| 137 | + expect(redis.keys("graphql-channel:*").count).to be > 0 |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + describe ".clean_fingerprint_subscriptions" do |
| 143 | + context "when subscription is blank" do |
| 144 | + subject do |
| 145 | + AnycableSchema.subscriptions.delete_subscription(subscription_id) |
| 146 | + |
| 147 | + described_class.clean_fingerprint_subscriptions |
| 148 | + end |
| 149 | + |
| 150 | + it "cleans graphql-subscriptions" do |
| 151 | + subscriptions_key = redis.keys("graphql-subscriptions:*")[0] |
| 152 | + |
| 153 | + expect(redis.smembers(subscriptions_key).empty?).to be false |
| 154 | + |
| 155 | + subject |
| 156 | + |
| 157 | + expect(redis.smembers(subscriptions_key).empty?).to be true |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + describe ".clean_topic_fingerprints" do |
| 163 | + subject do |
| 164 | + # Emulate situation, when subscriptions in fingerprints are orphan |
| 165 | + redis.scan_each(match: "graphql-subscriptions:*").each do |k| |
| 166 | + redis.del(k) |
| 167 | + end |
| 168 | + |
| 169 | + described_class.clean_topic_fingerprints |
| 170 | + end |
| 171 | + |
| 172 | + it "cleans fingerprints" do |
| 173 | + expect(redis.zrange("graphql-fingerprints::productUpdated:", 0, -1).empty?).to be false |
| 174 | + |
| 175 | + subject |
| 176 | + |
| 177 | + expect(redis.zrange("graphql-fingerprints::productUpdated:", 0, -1).empty?).to be true |
| 178 | + end |
| 179 | + end |
| 180 | +end |
0 commit comments