Skip to content

Commit

Permalink
implement Timer#repeat and Timer#repeat=
Browse files Browse the repository at this point in the history
  • Loading branch information
qqshfox committed Apr 2, 2013
1 parent 827e08e commit d7f6aa3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
25 changes: 25 additions & 0 deletions ext/rbuv/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static void rbuv_timer_free(rbuv_timer_t *rbuv_timer);
/* Methods */
static VALUE rbuv_timer_start(VALUE self, VALUE timeout, VALUE repeat);
static VALUE rbuv_timer_stop(VALUE self);
static VALUE rbuv_timer_repeat_get(VALUE self);
static VALUE rbuv_timer_repeat_set(VALUE self, VALUE repeat);

/* Private methods */
static void _uv_timer_on_timeout(uv_timer_t *uv_timer, int status);
Expand All @@ -29,6 +31,8 @@ void Init_rbuv_timer() {

rb_define_method(cRbuvTimer, "start", rbuv_timer_start, 2);
rb_define_method(cRbuvTimer, "stop", rbuv_timer_stop, 0);
rb_define_method(cRbuvTimer, "repeat", rbuv_timer_repeat_get, 0);
rb_define_method(cRbuvTimer, "repeat=", rbuv_timer_repeat_set, 1);
}

VALUE rbuv_timer_alloc(VALUE klass) {
Expand Down Expand Up @@ -99,6 +103,27 @@ VALUE rbuv_timer_stop(VALUE self) {
return self;
}

VALUE rbuv_timer_repeat_get(VALUE self) {
rbuv_timer_t *rbuv_timer;

Data_Get_Struct(self, rbuv_timer_t, rbuv_timer);

return ULL2NUM(uv_timer_get_repeat(rbuv_timer->uv_handle));
}

VALUE rbuv_timer_repeat_set(VALUE self, VALUE repeat) {
rbuv_timer_t *rbuv_timer;
uint64_t uv_repeat;

uv_repeat = NUM2ULL(repeat);

Data_Get_Struct(self, rbuv_timer_t, rbuv_timer);

uv_timer_set_repeat(rbuv_timer->uv_handle, uv_repeat);

return repeat;
}

void _uv_timer_on_timeout(uv_timer_t *uv_timer, int status) {
VALUE timer;
rbuv_timer_t *rbuv_timer;
Expand Down
31 changes: 30 additions & 1 deletion spec/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
end

context "run in loop" do
after(:each) { Rbuv.run_loop }
before(:each) { @skip_running_loop = false }
after(:each) { Rbuv.run_loop unless @skip_running_loop }

context "when timeout == 0" do
context "#start" do
Expand Down Expand Up @@ -73,6 +74,34 @@
end
end

context "#repeat" do
[0, 10, 100].each do |repeat|
it "should eq #{repeat}" do
timer = Rbuv::Timer.new

timer.start 0, repeat do |t|
t.repeat.should eq repeat
t.stop
end
end
end
end

context "#repeat=" do
[0, 10, 100].each do |repeat|
it "should eq #{repeat}" do
timer = Rbuv::Timer.new

timer.start 0, 0 do |t|
t.repeat = repeat
end
Rbuv.run_loop
timer.repeat.should eq repeat
@skip_running_loop = true
end
end
end

context ".start" do
context 'be valid' do
it "when repeat == 0" do
Expand Down

0 comments on commit d7f6aa3

Please sign in to comment.