M lib/auto_top_up_repo.rb => lib/auto_top_up_repo.rb +9 -0
@@ 13,6 13,11 @@ class AutoTopUpRepo
def put(customer_id, amount)
if amount >= 15
redis(:set, customer_id, amount)
+ reset_low_balance_lock(customer_id)
+ @db.exec_params(
+ "SELECT check_and_notify_low_balance($1)",
+ [customer_id]
+ )
elsif amount.zero?
redis(:del, customer_id)
end
@@ 27,4 32,8 @@ protected
*args
)
end
+
+ def reset_low_balance_lock(customer_id)
+ @redis.del("jmp_customer_low_balance-#{customer_id}")
+ end
end
M test/test_auto_top_up_repo.rb => test/test_auto_top_up_repo.rb +11 -0
@@ 28,8 28,19 @@ class AutoTopUpRepoTest < Minitest::Test
nil,
["jmp_customer_auto_top_up_amount-somecustomer", amount]
)
+ @redis.expect(
+ :del,
+ nil,
+ ["jmp_customer_low_balance-somecustomer"]
+ )
+ @db.expect(
+ :exec_params,
+ nil,
+ ["SELECT check_and_notify_low_balance($1)", ["somecustomer"]]
+ )
@repo.put("somecustomer", amount)
assert_mock @redis
+ assert_mock @db
end
property(:put_invalid_amount) { branch [:range, 1, 14], [:range, -999, -1] }