Anahkiasen / underscore-php

A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel
http://anahkiasen.github.com/underscore-php/
1.12k stars 88 forks source link

Functions::throttle() is buggy #27

Open tortuetorche opened 10 years ago

tortuetorche commented 10 years ago

If you update the Functions::throttle() test like this (replace sleep(1); by sleep(2);:

  public function testCanThrottleFunctions()
  {
    $number = 0;
    $function = Functions::throttle(function () use (&$number) {
      $number++;
    }, 1);

    $function();
    $function();
    sleep(2);
    $function();

    $this->assertEquals(2, $number);
  }

PHPUnit returns: Failed asserting that 3 matches expected 2.

Or if you update the test like this (add a $this->assertEquals(1, $number); assertion):

  public function testCanThrottleFunctions()
  {
    $number = 0;
    $function = Functions::throttle(function () use (&$number) {
      $number++;
    }, 1);

    $function();
    $function();
    $this->assertEquals(1, $number);
    sleep(1);
    $function();

    $this->assertEquals(2, $number);
  }

PHPUnit returns: Failed asserting that 2 matches expected 1.

black-snow commented 10 years ago

Also there's a typo on http://anahkiasen.github.io/underscore-php/#Arrays within the throttle code example. Should be "Functions::throttle" instead of "Functions::only".

Anahkiasen commented 10 years ago

PRs accepted if you want to push a fix quickly