You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2019/12/12 23:57:35 UTC

[couchdb-smoosh] 02/02: Pass time into in_allowed_window

This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch utc-time
in repository https://gitbox.apache.org/repos/asf/couchdb-smoosh.git

commit 8f7fa38a4c7e2aa7dd548e4ad2bfea05edbba93b
Author: Jay Doane <ja...@apache.org>
AuthorDate: Thu Dec 12 15:56:55 2019 -0800

    Pass time into in_allowed_window
    
    Before this patch, the current time is calculated inside
    in_allowed_window/2, which makes testing it difficult, as the
    calculations depend on when tests are run.
    
    This change factors the time as an argument of in_allowed_window, while
    the tests continue to test against the current time that they are run.
---
 src/smoosh_channel.erl                 |  3 ++-
 src/smoosh_utils.erl                   | 19 +++++++------------
 test/exunit/scheduling_window_test.exs | 24 ++++++++++++++----------
 3 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/smoosh_channel.erl b/src/smoosh_channel.erl
index d8a8d14..26dce16 100644
--- a/src/smoosh_channel.erl
+++ b/src/smoosh_channel.erl
@@ -156,7 +156,8 @@ handle_info(check_window, State0) ->
     {ok, State} = code_change(nil, State0, nil),
     #state{paused = Paused, name = Name} = State,
     StrictWindow = smoosh_utils:get(Name, "strict_window", "false"),
-    FinalState = case {not Paused, smoosh_utils:in_allowed_window(Name)} of
+    {_, {HH, MM, _}} = calendar:universal_time(),
+    FinalState = case {not Paused, smoosh_utils:in_allowed_window({HH, MM}, Name)} of
         {false, false} ->
             % already in desired state
             State;
diff --git a/src/smoosh_utils.erl b/src/smoosh_utils.erl
index fcd0fcd..ca8c271 100644
--- a/src/smoosh_utils.erl
+++ b/src/smoosh_utils.erl
@@ -15,7 +15,7 @@
 
 -export([get/2, get/3, group_pid/1, split/1, stringify/1, ignore_db/1]).
 -export([
-    in_allowed_window/1
+    in_allowed_window/2
 ]).
 
 group_pid({Shard, GroupId}) ->
@@ -60,20 +60,15 @@ ignore_db(DbName) when is_list(DbName) ->
 ignore_db(Db) ->
     ignore_db(couch_db:name(Db)).
 
-in_allowed_window(Channel) ->
+in_allowed_window(Time, Channel) ->
     From = parse_time(get(Channel, "from"), {00, 00}),
     To = parse_time(get(Channel, "to"), {24, 00}),
-    in_allowed_window(From, To).
-
-in_allowed_window(From, To) ->
-    {_, {HH, MM, _}} = calendar:universal_time(),
-    case From < To of
-    true ->
-        ({HH, MM} >= From) andalso ({HH, MM} < To);
-    false ->
-        ({HH, MM} >= From) orelse ({HH, MM} < To)
-    end.
+    in_allowed_window(Time, From, To).
 
+in_allowed_window({HH, MM}, From, To) when From < To ->
+    ({HH, MM} >= From) andalso ({HH, MM} < To);
+in_allowed_window({HH, MM}, From, To) ->
+    ({HH, MM} >= From) orelse ({HH, MM} < To).
 
 parse_time(undefined, Default) ->
     Default;
diff --git a/test/exunit/scheduling_window_test.exs b/test/exunit/scheduling_window_test.exs
index 7fa6c23..e9c3d4c 100644
--- a/test/exunit/scheduling_window_test.exs
+++ b/test/exunit/scheduling_window_test.exs
@@ -1,8 +1,6 @@
 defmodule SmooshSchedulingWindowTest do
   use Couch.Test.ExUnit.Case
 
-  alias Couch.Test.Setup
-
   setup_all(context) do
     test_ctx = :test_util.start_couch([])
 
@@ -15,14 +13,20 @@ defmodule SmooshSchedulingWindowTest do
     context
   end
 
+  def hh_mm(date_time) do
+    {date_time.hour, date_time.minute}
+  end
+
   test "in_allowed_window returns true by default", _context do
-    assert :smoosh_utils.in_allowed_window('nonexistent_channel') == true
+    now = DateTime.utc_now()
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'nonexistent_channel') == true
   end
 
   test "in_allowed_window ignores bad input", _context do
+    now = DateTime.utc_now()
     :config.set('smoosh.test_channel', 'from', 'midnight', false)
     :config.set('smoosh.test_channel', 'to', 'infinity', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == true
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == true
   end
 
   test "in_allowed_window returns false when now < from < to", _context do
@@ -31,7 +35,7 @@ defmodule SmooshSchedulingWindowTest do
     to = DateTime.add(now, 36_000)
     :config.set('smoosh.test_channel', 'from', '#{from.hour}:#{from.minute}', false)
     :config.set('smoosh.test_channel', 'to', '#{to.hour}:#{to.minute}', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == false
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == false
   end
 
   test "in_allowed_window returns true when from < now < to", _context do
@@ -40,7 +44,7 @@ defmodule SmooshSchedulingWindowTest do
     to = DateTime.add(now, 18_000)
     :config.set('smoosh.test_channel', 'from', '#{from.hour}:#{from.minute}', false)
     :config.set('smoosh.test_channel', 'to', '#{to.hour}:#{to.minute}', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == true
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == true
   end
 
   test "in_allowed_window returns false when from < to < now", _context do
@@ -49,7 +53,7 @@ defmodule SmooshSchedulingWindowTest do
     to = DateTime.add(now, -18_000)
     :config.set('smoosh.test_channel', 'from', '#{from.hour}:#{from.minute}', false)
     :config.set('smoosh.test_channel', 'to', '#{to.hour}:#{to.minute}', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == false
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == false
   end
 
   test "in_allowed_window returns true when to < from < now", _context do
@@ -58,7 +62,7 @@ defmodule SmooshSchedulingWindowTest do
     to = DateTime.add(now, -36_000)
     :config.set('smoosh.test_channel', 'from', '#{from.hour}:#{from.minute}', false)
     :config.set('smoosh.test_channel', 'to', '#{to.hour}:#{to.minute}', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == true
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == true
   end
 
   test "in_allowed_window returns false when to < now < from", _context do
@@ -67,7 +71,7 @@ defmodule SmooshSchedulingWindowTest do
     to = DateTime.add(now, -18_000)
     :config.set('smoosh.test_channel', 'from', '#{from.hour}:#{from.minute}', false)
     :config.set('smoosh.test_channel', 'to', '#{to.hour}:#{to.minute}', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == false
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == false
   end
 
   test "in_allowed_window returns true when now < to < from", _context do
@@ -76,6 +80,6 @@ defmodule SmooshSchedulingWindowTest do
     to = DateTime.add(now, 18_000)
     :config.set('smoosh.test_channel', 'from', '#{from.hour}:#{from.minute}', false)
     :config.set('smoosh.test_channel', 'to', '#{to.hour}:#{to.minute}', false)
-    assert :smoosh_utils.in_allowed_window('test_channel') == true
+    assert :smoosh_utils.in_allowed_window(hh_mm(now), 'test_channel') == true
   end
 end