Skip to content

Commit

Permalink
Raise exception if await call twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Nov 12, 2023
1 parent 459efb6 commit 1666902
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Layout/RedundantLineBreak:
Lint/ConstantResolution:
Enabled: false

Lint/EmptyBlock:
Enabled: false

Metrics/AbcSize:
Enabled: false

Expand Down
5 changes: 5 additions & 0 deletions ext/ilios/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,16 @@ static VALUE future_await(VALUE self)

GET_FUTURE(self, cassandra_future);

if (cassandra_future->already_waited) {
rb_raise(eExecutionError, "It should not call twice");
}

if (cassandra_future->on_success_block || cassandra_future->on_failure_block) {
nogvl_sem_wait(&cassandra_future->sem);
} else {
nogvl_future_wait(cassandra_future->future);
}
cassandra_future->already_waited = true;
return self;
}

Expand Down
1 change: 1 addition & 0 deletions ext/ilios/ilios.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typedef struct
VALUE proc_mutex;

uv_sem_t sem;
bool already_waited;
} CassandraFuture;

extern const rb_data_type_t cassandra_session_data_type;
Expand Down
2 changes: 2 additions & 0 deletions ext/ilios/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static VALUE session_prepare_async(VALUE self, VALUE query)
cassandra_future->session_obj = self;
cassandra_future->proc_mutex = rb_mutex_new();
uv_sem_init(&cassandra_future->sem, 0);
cassandra_future->already_waited = false;

return cassandra_future_obj;
}
Expand Down Expand Up @@ -109,6 +110,7 @@ static VALUE session_execute_async(VALUE self, VALUE statement)
cassandra_future->statement_obj = statement;
cassandra_future->proc_mutex = rb_mutex_new();
uv_sem_init(&cassandra_future->sem, 0);
cassandra_future->already_waited = false;

return cassandra_future_obj;
}
Expand Down
11 changes: 6 additions & 5 deletions test/test_future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_await
prepare_future.await

assert_equal(50, count)
assert_raises(Ilios::Cassandra::ExecutionError) { prepare_future.await }
end

def test_on_success
Expand All @@ -60,9 +61,9 @@ def test_on_success

assert_raises(ArgumentError) { future.on_success }

future.on_success { }
future.on_success {}

assert_raises(Ilios::Cassandra::ExecutionError) { future.on_success { } }
assert_raises(Ilios::Cassandra::ExecutionError) { future.on_success {} }
end

def test_on_failure
Expand All @@ -71,8 +72,8 @@ def test_on_failure

assert_raises(ArgumentError) { future.on_failure }

future.on_failure { }
future.on_failure {}

assert_raises(Ilios::Cassandra::ExecutionError) { future.on_failure { } }
end
assert_raises(Ilios::Cassandra::ExecutionError) { future.on_failure {} }
end
end

0 comments on commit 1666902

Please sign in to comment.