diff --git a/ext/-test-/postponed_job/postponed_job.c b/ext/-test-/postponed_job/postponed_job.c index b79b70efb1..9ac866ae77 100644 --- a/ext/-test-/postponed_job/postponed_job.c +++ b/ext/-test-/postponed_job/postponed_job.c @@ -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); } diff --git a/test/-ext-/postponed_job/test_postponed_job.rb b/test/-ext-/postponed_job/test_postponed_job.rb index 6831ef7b37..8c2b3e95d1 100644 --- a/test/-ext-/postponed_job/test_postponed_job.rb +++ b/test/-ext-/postponed_job/test_postponed_job.rb @@ -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')