Add a test case for preregistering with different data

We want to make sure that if preregister is called with different data,
that the postponed job table is updated.
This commit is contained in:
KJ Tsanaktsidis 2023-12-11 15:22:42 +11:00 коммит произвёл KJ Tsanaktsidis
Родитель 15d14e2f39
Коммит a8d2d93aff
2 изменённых файлов: 45 добавлений и 0 удалений

Просмотреть файл

@ -176,6 +176,42 @@ pjob_preregister_multiple_times(VALUE self)
}
struct pjob_append_data_args {
VALUE ary;
VALUE data;
};
static void
pjob_append_data_callback(void *vctx) {
struct pjob_append_data_args *ctx = (struct pjob_append_data_args *)vctx;
Check_Type(ctx->ary, T_ARRAY);
rb_ary_push(ctx->ary, ctx->data);
}
static VALUE
pjob_preregister_calls_with_last_argument(VALUE self)
{
VALUE ary = rb_ary_new();
struct pjob_append_data_args arg1 = { .ary = ary, .data = INT2FIX(1) };
struct pjob_append_data_args arg2 = { .ary = ary, .data = INT2FIX(2) };
struct pjob_append_data_args arg3 = { .ary = ary, .data = INT2FIX(3) };
struct pjob_append_data_args arg4 = { .ary = ary, .data = INT2FIX(4) };
rb_postponed_job_handle_t h;
h = rb_postponed_job_preregister(0, pjob_append_data_callback, &arg1);
rb_postponed_job_preregister(0, pjob_append_data_callback, &arg2);
rb_postponed_job_trigger(h);
rb_postponed_job_preregister(0, pjob_append_data_callback, &arg3);
rb_thread_sleep(0); // should execute with arg3
rb_postponed_job_preregister(0, pjob_append_data_callback, &arg4);
rb_postponed_job_trigger(h);
rb_thread_sleep(0); // should execute with arg4
return ary;
}
void
Init_postponed_job(VALUE self)
{
@ -190,5 +226,6 @@ Init_postponed_job(VALUE self)
rb_define_module_function(mBug, "postponed_job_preregister_and_call_with_sleep", pjob_preregister_and_call_with_sleep, 1);
rb_define_module_function(mBug, "postponed_job_preregister_and_call_without_sleep", pjob_preregister_and_call_without_sleep, 1);
rb_define_module_function(mBug, "postponed_job_preregister_multiple_times", pjob_preregister_multiple_times, 0);
rb_define_module_function(mBug, "postponed_job_preregister_calls_with_last_argument", pjob_preregister_calls_with_last_argument, 0);
}

Просмотреть файл

@ -25,6 +25,14 @@ class TestPostponed_job < Test::Unit::TestCase
RUBY
end
def test_multiple_preregistration_with_new_data
assert_separately([], __FILE__, __LINE__, <<-'RUBY')
require '-test-/postponed_job'
values = Bug.postponed_job_preregister_calls_with_last_argument
# i.e. the callback is called with the last argument it was preregistered with
assert_equal [3, 4], values
RUBY
end
def test_legacy_register
assert_separately([], __FILE__, __LINE__, <<-'RUBY')