Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix Redis::Client#transaction: wrt Exception handling and add tests

  • Loading branch information...
commit 518b3d8450ef1aa2dbc08080063db9f06af4c43f 1 parent 4af5b19
@bakkdoor authored
Showing with 41 additions and 3 deletions.
  1. +4 −3 lib/redis/client.fy
  2. +36 −0 tests/client.fy
  3. +1 −0  tests/test_helper.fy
View
7 lib/redis/client.fy
@@ -98,15 +98,16 @@ class Redis {
def transaction: block {
try {
- call: ['multi]
+ call: 'multi
block call: [self]
- } catch {
+ call: 'exec
} finally {
- call: ['exec]
+ call: 'discard
}
}
def call: command {
+ command = command to_a
cmd_name = command first
match cmd_name {
View
36 tests/client.fy
@@ -0,0 +1,36 @@
+FancySpec describe: Redis Client with: {
+ before_each: {
+ @r = Redis Client new
+ }
+
+ it: "performs a correct call" with: 'call: when: {
+ @r call: ['get, 'foo] . is: nil
+ @r call: ['set, 'foo, "bar"] . is: "OK"
+ @r call: ['get, 'foo] . is: "bar"
+ @r call: ['del, 'foo] . is: 1
+ @r call: ['del, 'foo] . is: 0
+ }
+
+ it: "performs the transaction correctly" with: 'transaction: when: {
+ @r transaction: @{
+ call: ['get, 'foo] . is: "QUEUED"
+ call: ['set, 'foo, "bar"] . is: "QUEUED"
+ call: ['get, 'foo] . is: "QUEUED"
+ }
+ @r call: ['get, 'foo] . is: "bar"
+ @r call: ['del, 'foo] . is: 1
+ }
+ it: "discards a transaction when an Exception is raised" with: 'transaction: when: {
+ @r call: ['get, 'foo] . is: nil
+ {
+ @r transaction: @{
+ call: ['get, 'foo] . is: "QUEUED"
+ 2 / 0 # raise an error
+ call: ['set, 'foo, "bar"] . is: "QUEUED"
+ }
+ } raises: ZeroDivisionError
+
+ # did not save due to Exception:
+ @r call: ['get, 'foo] . is: nil
+ }
+}
View
1  tests/test_helper.fy
@@ -0,0 +1 @@
+require: "lib/redis"
Please sign in to comment.
Something went wrong with that request. Please try again.