GithubHelp home page GithubHelp logo

spatulasnout / em_test_runner Goto Github PK

View Code? Open in Web Editor NEW
3.0 5.0 0.0 13 KB

Ruby test framework similar to test/unit, but which works with EventMachine and asynchronous block/proc callbacks.

License: BSD 3-Clause "New" or "Revised" License

Ruby 100.00%

em_test_runner's Introduction

Program: em_test_runner.rb
Purpose: em_test_runner is a ruby unit test framework which is similar to
test/unit, but which is designed to work with EventMachine.
Author: Bill Kelly, 2009
License: Released into the public domain (NO WARRANTY)

Example of difficulty combining traditional test/unit and EM:

  def test_foo
    EM.add_timer(0.5) do
      assert( "kazango" )
    end
  end

`test_foo' will complete immediately from the point of view of test/unit,
whereas we would prefer it wait until the EM timer callback occurred.

In em_test_runner, the above is rewritten as:

  def test_foo(completed)
    EM.add_timer(0.5) do
      assert( "kazango" )
      completed.call  # let the framework know we're done
    end
  end


Further examples:

  class FooBase < TestEM::TestCase
    # no tests defined here, but not considered an error,
    # because there are derived classes
  end
  
  class Foo < FooBase
    def test_simple(completed)
      assert(true)
      completed.call
    end
    
    def test_deferred(completed)
      assert_equal("Humbert", "Humbert")
      EM.add_timer(0.25) do
        assert(true)
        completed.call
      end
    end
  end

  class Bar < TestEM::TestCase
    # no tests defined (error)
  end

  class Baz < TestEM::TestCase
    def test_timeout(completed)
      set_timeout(1)
      # we never call `completed' , so this
      # test will fail with a timeout
      # NOTE: timeout is reset to default via 
      # set_timeout(default_timeout_secs)
      # before each test_ invocation.
      # (default can be overridden.)
    end    
  end


em_test_runner runs all the test suites in a single 
EventMachine.run { } invocation, no need to start/stop
the reactor between tests.

Further, if you have a need to perform some one-time 
init after EM.run { } but prior to any tests being executed,
a hook mechanism is provided:

  TestEM::Runner.after_em_start do |completed|
    $initcb = "uno "
    EM.add_timer(0.25) do
      $initcb << "tres."
      completed.call
    end
  end

  TestEM::Runner.after_em_start do |completed|
    $initcb << "dos "
    completed.call
  end

$initcb will be "uno dos tres." after the init blocks run
in sequence.


Enjoy,

Bill

em_test_runner's People

Contributors

kbaker avatar spatulasnout avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.