pusher-fake 4.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d87ee628168fd524b482569791802df7bb50695dc7cfc4f74164587b3a195df
4
- data.tar.gz: dec3b6f7071488cf6add4418fe6e9a8a18c0b778edcc2298b22e529e9f3ad3eb
3
+ metadata.gz: '039808605b16ad3d4532da9fc4041f4f2cba3f783e4ec63729f5bc5b024565eb'
4
+ data.tar.gz: 4ff72ecce469d90b48892ae1fe0f677806a9aa8b8a3b0ff5a33c7105da3585b3
5
5
  SHA512:
6
- metadata.gz: f2ca4fc45b9710725d542a7c73324b7a995b36d896c72fb7561f5cd3544b548a26f07438896530ce60e4a1689be4bbe99cc77361747f4779b3f77b38900f9ad3
7
- data.tar.gz: fc90fef11fb8d9d0838f3597521610a77822f3ebd84ed82554ac8dbe3098f91b6c94afdb699c8c41e47d758ecac1ea38abdb1a1a89a26e56a8753a1758bb7aff
6
+ metadata.gz: 7036cbe9c7d805d193ccbb6492a58ef102e793218907787df1a8a2155a828e10aab13483ec6d783a65a1b11dc8936e73f8d3f55735cf64bb948a590dfe4e238a
7
+ data.tar.gz: ecf4faa213465f21879a0926c9e2adf8d1b80e515eff2a2192c8033e998bcbaca044e770c54652d6a31c4f967cc88f4c7c4a9cb3b04c601eb68016a964140c84
@@ -4,6 +4,8 @@ module PusherFake
4
4
  module Channel
5
5
  # A public channel.
6
6
  class Public
7
+ CACHE_CHANNEL_PREFIX = /^(private-|presence-){0,1}cache-/.freeze
8
+
7
9
  # @return [Array] Connections in this channel.
8
10
  attr_reader :connections
9
11
 
@@ -15,6 +17,7 @@ module PusherFake
15
17
  # @param [String] name The channel name.
16
18
  def initialize(name)
17
19
  @name = name
20
+ @last_event = nil
18
21
  @connections = []
19
22
  end
20
23
 
@@ -24,6 +27,7 @@ module PusherFake
24
27
  # @param [Hash] options The options for the channel.
25
28
  def add(connection, options = {})
26
29
  subscription_succeeded(connection, options)
30
+ emit_last_event(connection)
27
31
  end
28
32
 
29
33
  # Emit an event to the channel.
@@ -31,6 +35,10 @@ module PusherFake
31
35
  # @param [String] event The event name.
32
36
  # @param [Hash] data The event data.
33
37
  def emit(event, data, options = {})
38
+ if cache_channel?
39
+ @last_event = [event, data]
40
+ end
41
+
34
42
  connections.each do |connection|
35
43
  unless connection.id == options[:socket_id]
36
44
  connection.emit(event, data, name)
@@ -71,6 +79,30 @@ module PusherFake
71
79
 
72
80
  private
73
81
 
82
+ # @return [Array] Arguments for the last event emitted.
83
+ attr_reader :last_event
84
+
85
+ # Whether or not the channel is a cache channel.
86
+ #
87
+ # @return [Boolean]
88
+ def cache_channel?
89
+ @cache_channel ||= name.match?(CACHE_CHANNEL_PREFIX)
90
+ end
91
+
92
+ # Emit the last event if present and a cache channel.
93
+ #
94
+ # @param [Connection] connection The connection to emit to.
95
+ def emit_last_event(connection)
96
+ return unless cache_channel?
97
+
98
+ if last_event
99
+ connection.emit(*last_event, name)
100
+ else
101
+ connection.emit("pusher:cache_miss", nil, name)
102
+ trigger("cache_miss", channel: name)
103
+ end
104
+ end
105
+
74
106
  # Notify the +connection+ of the successful subscription and add the
75
107
  # connection to the channel.
76
108
  #
data/lib/pusher-fake.rb CHANGED
@@ -9,7 +9,7 @@ require "thin"
9
9
  # A Pusher fake.
10
10
  module PusherFake
11
11
  # The current version string.
12
- VERSION = "4.0.0"
12
+ VERSION = "4.1.0"
13
13
 
14
14
  autoload :Channel, "pusher-fake/channel"
15
15
  autoload :Configuration, "pusher-fake/configuration"
@@ -3,6 +3,10 @@
3
3
  require "spec_helper"
4
4
 
5
5
  feature "Client subscribing to a channel" do
6
+ let(:cache_event) { "command" }
7
+ let(:cache_channel) { "cache-last-command" }
8
+ let(:other_user) { "Bob" }
9
+
6
10
  before do
7
11
  connect
8
12
  end
@@ -49,16 +53,27 @@ feature "Client subscribing to a channel" do
49
53
  expect(page).not_to have_content("Subscribed to presence-game-1.")
50
54
  end
51
55
 
52
- protected
56
+ scenario "successfully subscribes to a cache channel, with no cached event" do
57
+ subscribe_to(cache_channel)
53
58
 
54
- def attempt_to_subscribe_to(channel)
55
- page.execute_script("Helpers.subscribe(#{MultiJson.dump(channel)})")
59
+ expect(page).to have_content("No cached event for cache-last-command.")
56
60
  end
57
61
 
58
- def connect
59
- visit "/"
62
+ scenario "successfully subscribes to a cache channel, with cached event" do
63
+ subscribe_to(cache_channel)
64
+ Pusher.trigger(cache_channel, cache_event, {}, {})
65
+
66
+ connect_as(other_user, channel: cache_channel)
60
67
 
61
- expect(page).to have_content("Client connected.")
68
+ using_session(other_user) do
69
+ expect(page).to have_content("Channel #{cache_channel} received #{cache_event} event.")
70
+ end
71
+ end
72
+
73
+ protected
74
+
75
+ def attempt_to_subscribe_to(channel)
76
+ page.execute_script("Helpers.subscribe(#{MultiJson.dump(channel)})")
62
77
  end
63
78
 
64
79
  def override_socket_id(value)
@@ -5,6 +5,7 @@ require "spec_helper"
5
5
  feature "Receiving event webhooks" do
6
6
  let(:channel) { "room-1" }
7
7
  let(:other_user) { "Bob" }
8
+ let(:cache_channel) { "cache-last-command" }
8
9
  let(:presence_channel) { "presence-room-1" }
9
10
 
10
11
  before do
@@ -72,6 +73,24 @@ feature "Receiving event webhooks" do
72
73
  "user_id" => user_id(other_user))
73
74
  end
74
75
 
76
+ scenario "subscribing to a cache channel with no event" do
77
+ subscribe_to(cache_channel)
78
+
79
+ expect(events).to include_event("cache_miss",
80
+ "channel" => cache_channel)
81
+ end
82
+
83
+ scenario "subscribing to a cache channel with an event" do
84
+ subscribe_to(cache_channel)
85
+ events.clear
86
+ Pusher.trigger(cache_channel, "an event", {}, {})
87
+
88
+ subscribe_to_as(cache_channel, other_user)
89
+
90
+ expect(events).not_to include_event("cache_miss",
91
+ "channel" => cache_channel)
92
+ end
93
+
75
94
  protected
76
95
 
77
96
  def events
@@ -53,6 +53,64 @@ describe PusherFake::Channel, "#add" do
53
53
  expect(PusherFake::Webhook).to have_received(:trigger)
54
54
  .with("channel_occupied", channel: name).once
55
55
  end
56
+
57
+ context "when a cached channel" do
58
+ let(:name) { "cache-name" }
59
+
60
+ before do
61
+ allow(subject).to receive(:connections).and_call_original
62
+ end
63
+
64
+ context "with a cached event" do
65
+ before do
66
+ subject.emit("example-cache-event", example: true)
67
+ end
68
+
69
+ it "triggers the cached event to the connection added" do
70
+ subject.add(connection)
71
+
72
+ expect(connection).to have_received(:emit)
73
+ .with("example-cache-event", { example: true }, name).once
74
+ end
75
+
76
+ it "does not trigger cache miss event" do
77
+ subject.add(connection)
78
+
79
+ expect(connection).not_to have_received(:emit)
80
+ .with("pusher:cache-miss", nil, name)
81
+ end
82
+
83
+ it "does not trigger cache miss webhook" do
84
+ subject.add(connection)
85
+
86
+ expect(PusherFake::Webhook).not_to have_received(:trigger)
87
+ .with("cache_miss", channel: name)
88
+ end
89
+ end
90
+
91
+ context "without a cached event" do
92
+ it "triggers cache miss event" do
93
+ subject.add(connection)
94
+
95
+ expect(connection).to have_received(:emit)
96
+ .with("pusher:cache_miss", nil, name).once
97
+ end
98
+
99
+ it "triggers cache miss webhook" do
100
+ subject.add(connection)
101
+
102
+ expect(PusherFake::Webhook).to have_received(:trigger)
103
+ .with("cache_miss", channel: name).once
104
+ end
105
+
106
+ it "does not trigger event" do
107
+ subject.add(connection)
108
+
109
+ expect(connection).not_to have_received(:emit)
110
+ .with("example-cache-event", { example: true }, name)
111
+ end
112
+ end
113
+ end
56
114
  end
57
115
 
58
116
  describe PusherFake::Channel, "#emit" do
@@ -65,6 +65,9 @@
65
65
 
66
66
  list.removeChild(item);
67
67
  })
68
+ .bind("pusher:cache_miss", function(client) {
69
+ Helpers.log("No cached event for " + name + ".");
70
+ })
68
71
  .bind_global(function(event, message) {
69
72
  Helpers.log("Channel " + name + " received " + event + " event.");
70
73
  });
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pusher-fake
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tristan Dunn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-06 00:00:00.000000000 Z
11
+ date: 2022-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: em-http-request