Skip to content

Commit

Permalink
Fix debounce logic (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude authored Feb 7, 2022
1 parent 52f37f8 commit 9a50712
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 83 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [6.0.4] - 2022-02-05
### Fixed

- `Debouncer` calculates proper `next_sync_time`

## [6.0.3] - 2021-12-09
### Fixed
Fixed bug when routing key is nil.
Expand Down Expand Up @@ -45,7 +50,7 @@ Separate classes for publishing, object data, Rabbit params, debounce, serializa

6. Changed ORMAdapters.

7. Destroyed objects are initialized.
7. Destroyed objects are initialized.
Now custom attributes for destruction will be called on instances.
- Obj.table_sync_destroy_attributes() -> Obj#attributes_for_destroy

Expand Down
132 changes: 64 additions & 68 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
table_sync (6.0.3)
table_sync (6.0.4)
memery
rabbit_messaging
rails
Expand All @@ -10,60 +10,60 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.4.1)
actionpack (= 6.1.4.1)
activesupport (= 6.1.4.1)
actioncable (6.1.4.4)
actionpack (= 6.1.4.4)
activesupport (= 6.1.4.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.4.1)
actionpack (= 6.1.4.1)
activejob (= 6.1.4.1)
activerecord (= 6.1.4.1)
activestorage (= 6.1.4.1)
activesupport (= 6.1.4.1)
actionmailbox (6.1.4.4)
actionpack (= 6.1.4.4)
activejob (= 6.1.4.4)
activerecord (= 6.1.4.4)
activestorage (= 6.1.4.4)
activesupport (= 6.1.4.4)
mail (>= 2.7.1)
actionmailer (6.1.4.1)
actionpack (= 6.1.4.1)
actionview (= 6.1.4.1)
activejob (= 6.1.4.1)
activesupport (= 6.1.4.1)
actionmailer (6.1.4.4)
actionpack (= 6.1.4.4)
actionview (= 6.1.4.4)
activejob (= 6.1.4.4)
activesupport (= 6.1.4.4)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.4.1)
actionview (= 6.1.4.1)
activesupport (= 6.1.4.1)
actionpack (6.1.4.4)
actionview (= 6.1.4.4)
activesupport (= 6.1.4.4)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.4.1)
actionpack (= 6.1.4.1)
activerecord (= 6.1.4.1)
activestorage (= 6.1.4.1)
activesupport (= 6.1.4.1)
actiontext (6.1.4.4)
actionpack (= 6.1.4.4)
activerecord (= 6.1.4.4)
activestorage (= 6.1.4.4)
activesupport (= 6.1.4.4)
nokogiri (>= 1.8.5)
actionview (6.1.4.1)
activesupport (= 6.1.4.1)
actionview (6.1.4.4)
activesupport (= 6.1.4.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.4.1)
activesupport (= 6.1.4.1)
activejob (6.1.4.4)
activesupport (= 6.1.4.4)
globalid (>= 0.3.6)
activemodel (6.1.4.1)
activesupport (= 6.1.4.1)
activerecord (6.1.4.1)
activemodel (= 6.1.4.1)
activesupport (= 6.1.4.1)
activestorage (6.1.4.1)
actionpack (= 6.1.4.1)
activejob (= 6.1.4.1)
activerecord (= 6.1.4.1)
activesupport (= 6.1.4.1)
activemodel (6.1.4.4)
activesupport (= 6.1.4.4)
activerecord (6.1.4.4)
activemodel (= 6.1.4.4)
activesupport (= 6.1.4.4)
activestorage (6.1.4.4)
actionpack (= 6.1.4.4)
activejob (= 6.1.4.4)
activerecord (= 6.1.4.4)
activesupport (= 6.1.4.4)
marcel (~> 1.0.0)
mini_mime (>= 1.1.0)
activesupport (6.1.4.1)
activesupport (6.1.4.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand All @@ -84,16 +84,13 @@ GEM
diff-lcs (1.4.4)
docile (1.3.5)
erubi (1.10.0)
exception_notification (4.4.3)
actionmailer (>= 4.0, < 7)
activesupport (>= 4.0, < 7)
globalid (0.5.2)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.8.10)
i18n (1.9.1)
concurrent-ruby (~> 1.0)
lamian (1.4.0)
lamian (1.5.0)
rails (>= 4.2)
loofah (2.12.0)
loofah (2.13.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
Expand All @@ -104,7 +101,7 @@ GEM
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.6.1)
minitest (5.14.4)
minitest (5.15.0)
nio4r (2.5.8)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
Expand All @@ -116,9 +113,8 @@ GEM
pry (0.14.0)
coderay (~> 1.1)
method_source (~> 1.0)
rabbit_messaging (0.12.0)
rabbit_messaging (0.12.1)
bunny (~> 2.0)
exception_notification
lamian
rails (>= 5.2)
sneakers (~> 2.0)
Expand All @@ -127,35 +123,35 @@ GEM
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.1.4.1)
actioncable (= 6.1.4.1)
actionmailbox (= 6.1.4.1)
actionmailer (= 6.1.4.1)
actionpack (= 6.1.4.1)
actiontext (= 6.1.4.1)
actionview (= 6.1.4.1)
activejob (= 6.1.4.1)
activemodel (= 6.1.4.1)
activerecord (= 6.1.4.1)
activestorage (= 6.1.4.1)
activesupport (= 6.1.4.1)
rails (6.1.4.4)
actioncable (= 6.1.4.4)
actionmailbox (= 6.1.4.4)
actionmailer (= 6.1.4.4)
actionpack (= 6.1.4.4)
actiontext (= 6.1.4.4)
actionview (= 6.1.4.4)
activejob (= 6.1.4.4)
activemodel (= 6.1.4.4)
activerecord (= 6.1.4.4)
activestorage (= 6.1.4.4)
activesupport (= 6.1.4.4)
bundler (>= 1.15.0)
railties (= 6.1.4.1)
railties (= 6.1.4.4)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.2)
loofah (~> 2.3)
railties (6.1.4.1)
actionpack (= 6.1.4.1)
activesupport (= 6.1.4.1)
railties (6.1.4.4)
actionpack (= 6.1.4.4)
activesupport (= 6.1.4.4)
method_source
rake (>= 0.13)
thor (~> 1.0)
rainbow (3.0.0)
rake (13.0.6)
rbtree (0.4.4)
rbtree (0.4.5)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec (3.10.0)
Expand Down Expand Up @@ -230,21 +226,21 @@ GEM
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.4.1)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
tainbox (2.1.2)
activesupport
thor (1.1.0)
thor (1.2.1)
timecop (0.9.4)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)
zeitwerk (2.5.4)

PLATFORMS
ruby
Expand Down
8 changes: 4 additions & 4 deletions lib/table_sync/publishing/helpers/debounce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def initialize(object_class:, needle:, event:, debounce_time: nil)
end

def skip?
true if sync_in_the_future? && upsert_event? # case 3.1
sync_in_the_future? && upsert_event? # case 3.1
end

memoize def next_sync_time
Expand All @@ -87,16 +87,16 @@ def sync_in_the_past?
end

def debounce_time_passed?
cached_sync_time + debounce_time.seconds >= current_time
cached_sync_time + debounce_time.seconds <= current_time
end

def debounce_time_not_passed?
cached_sync_time + debounce_time.seconds < current_time
cached_sync_time + debounce_time.seconds > current_time
end

# CASE 3
def sync_in_the_future?
cached_sync_time && (cached_sync_time > current_time)
!!cached_sync_time && (cached_sync_time > current_time)
end

def destroy_event?
Expand Down
2 changes: 1 addition & 1 deletion lib/table_sync/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module TableSync
VERSION = "6.0.3"
VERSION = "6.0.4"
end
18 changes: 9 additions & 9 deletions spec/publishing/helpers/debounce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,35 @@ def set_cached_sync_time(time)
let(:debounce_time) { 0 }
let(:expected_time) { current_time }

include_examples "skip? returns", nil
include_examples "skip? returns", false
include_examples "returns correct next_sync_time"
end

context "case 1: cached sync time is empty" do
let(:expected_time) { current_time }

include_examples "skip? returns", nil
include_examples "skip? returns", false
include_examples "returns correct next_sync_time"
end

context "case 2: cached sync time in the past" do
context "case 2.1: debounce time passed" do
let(:expected_time) { current_time }
let(:cached_time) { current_time - debounce_time.seconds + 10.seconds }
let(:cached_time) { current_time - 30.seconds }

before { set_cached_sync_time(cached_time) }

include_examples "skip? returns", nil
include_examples "skip? returns", false
include_examples "returns correct next_sync_time"
end

context "case 2.2: debounce time not passed yet" do
let(:expected_time) { cached_time + debounce_time.seconds }
let(:cached_time) { current_time - debounce_time.seconds - 10.seconds }
let(:expected_time) { cached_time + 30.seconds }
let(:cached_time) { current_time }

before { set_cached_sync_time(cached_time) }

include_examples "skip? returns", nil
include_examples "skip? returns", false
include_examples "returns correct next_sync_time"
end
end
Expand All @@ -94,9 +94,9 @@ def set_cached_sync_time(time)

context "case 3.2: event destroy" do
let(:event) { :destroy }
let(:expected_time) { cached_time + debounce_time.seconds }
let(:expected_time) { cached_time + 30.seconds }

include_examples "skip? returns", nil
include_examples "skip? returns", false
include_examples "returns correct next_sync_time"
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
config.filter_run_when_matching :focus
config.default_formatter = "doc" if config.files_to_run.one?
config.expose_dsl_globally = true
config.profile_examples = 10

config.expect_with(:rspec) do |expectations|
expectations.syntax = :expect
Expand Down

0 comments on commit 9a50712

Please sign in to comment.