Skip to content

Commit 8a191f1

Browse files
author
Michael Kipper
committed
Review comments
1 parent 3c070d3 commit 8a191f1

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

ext/semian/resource.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ check_quota_arg(VALUE quota);
1212
static int
1313
check_tickets_arg(VALUE tickets);
1414

15+
static int
16+
check_min_tickets_arg(VALUE min_tickets);
17+
1518
static long
1619
check_permissions_arg(VALUE permissions);
1720

@@ -209,7 +212,7 @@ semian_resource_initialize(VALUE self, VALUE id, VALUE tickets, VALUE quota, VAL
209212
long c_permissions = check_permissions_arg(permissions);
210213
const char *c_id_str = check_id_arg(id);
211214
double c_timeout = check_default_timeout_arg(default_timeout);
212-
int c_min_tickets = check_tickets_arg(min_tickets);
215+
int c_min_tickets = check_min_tickets_arg(min_tickets);
213216

214217
// Build semian resource structure
215218
semian_resource_t *res = NULL;
@@ -303,6 +306,32 @@ check_tickets_arg(VALUE tickets)
303306
return c_tickets;
304307
}
305308

309+
static int
310+
check_min_tickets_arg(VALUE min_tickets)
311+
{
312+
int retval = -1;
313+
314+
switch (rb_type(min_tickets)) {
315+
case T_NIL:
316+
case T_UNDEF:
317+
return -1;
318+
case T_FLOAT:
319+
rb_warn("semian min_tickets value %f is a float, converting to fixnum", RFLOAT_VALUE(min_tickets));
320+
retval = (int) RFLOAT_VALUE(min_tickets);
321+
break;
322+
case T_FIXNUM:
323+
retval = FIX2LONG(min_tickets); break;
324+
default:
325+
retval = -1; break;
326+
}
327+
328+
if (retval <= 0 || retval > system_max_semaphore_count) {
329+
rb_raise(rb_eArgError, "max_tickets must be in range [1,%d)", system_max_semaphore_count);
330+
}
331+
332+
return retval;
333+
}
334+
306335
static const char*
307336
check_id_arg(VALUE id)
308337
{

ext/semian/tickets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ max(const int a, const int b)
8080
}
8181

8282
static int
83-
calculate_quota_tickets (int sem_id, double quota, int min_tickets)
83+
calculate_quota_tickets(int sem_id, double quota, int min_tickets)
8484
{
8585
int workers = get_sem_val(sem_id, SI_SEM_REGISTERED_WORKERS);
8686
int tickets = (int) ceil(workers * quota);

test/resource_test.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,14 @@ def test_min_tickets
506506
assert_equal(8, resource.tickets)
507507
end
508508

509+
def test_min_tickets_float
510+
expected_warning = /semian min_tickets value 2\.000000 is a float, converting to fixnum/
511+
with_fake_std_error(warn_message: expected_warning) do
512+
id = Time.now.strftime('%H:%M:%S.%N')
513+
Semian::Resource.new(id, quota: 0.49, timeout: 0.1, min_tickets: 2.0)
514+
end
515+
end
516+
509517
def test_min_tickets_nil
510518
id = Time.now.strftime('%H:%M:%S.%N')
511519
resource = Semian::Resource.new(id, quota: 0.49, timeout: 0.1, min_tickets: nil)
@@ -522,16 +530,9 @@ def test_min_tickets_nil
522530

523531
def test_min_tickets_zero
524532
id = Time.now.strftime('%H:%M:%S.%N')
525-
resource = Semian::Resource.new(id, quota: 0.49, timeout: 0.1, min_tickets: 0)
526-
assert_equal(1, resource.tickets)
527-
fork_workers(resource: id, count: 1, quota: 0.49, min_tickets: 0, timeout: 0.1, wait_for_timeout: true)
528-
assert_equal(1, resource.tickets)
529-
fork_workers(resource: id, count: 1, quota: 0.49, min_tickets: 0, timeout: 0.1, wait_for_timeout: true)
530-
assert_equal(2, resource.tickets)
531-
fork_workers(resource: id, count: 1, quota: 0.49, min_tickets: 0, timeout: 0.1, wait_for_timeout: true)
532-
assert_equal(2, resource.tickets)
533-
fork_workers(resource: id, count: 12, quota: 0.49, min_tickets: 0, timeout: 0.1, wait_for_timeout: true)
534-
assert_equal(8, resource.tickets)
533+
assert_raises ArgumentError do
534+
Semian::Resource.new(id, quota: 0.49, timeout: 0.1, min_tickets: 0)
535+
end
535536
end
536537

537538
def test_min_tickets_negative
@@ -541,6 +542,13 @@ def test_min_tickets_negative
541542
end
542543
end
543544

545+
def test_min_tickets_out_of_range
546+
id = Time.now.strftime('%H:%M:%S.%N')
547+
assert_raises ArgumentError do
548+
Semian::Resource.new(id, quota: 0.49, timeout: 0.1, min_tickets: 32768)
549+
end
550+
end
551+
544552
def create_resource(*args)
545553
@resources ||= []
546554
resource = Semian::Resource.new(*args)

0 commit comments

Comments
 (0)