Permalink
Browse files

all server tests are done

  • Loading branch information...
1 parent a420df6 commit 9c217d10dc459c55c999d3fb9a59b2c1472f5112 @txus committed Aug 27, 2011
View
4 Gemfile
@@ -2,3 +2,7 @@ source "http://rubygems.org"
# Specify your gem's dependencies in domodoro.gemspec
gemspec
+
+gem 'guard'
+gem 'guard-minitest'
+gem 'rb-fsevent'
View
5 Guardfile
@@ -0,0 +1,5 @@
+guard 'minitest' do
+ watch(%r|^test/(.*)_test\.rb|)
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
+ watch(%r|^test/test_helper\.rb|) { "test" }
+end
View
2 domodoro.gemspec
@@ -20,4 +20,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'eventmachine'
s.add_development_dependency 'minitest'
+ s.add_development_dependency 'mocha'
+ s.add_development_dependency 'purdytest'
end
View
69 lib/domodoro.rb
@@ -1,9 +1,13 @@
-require "domodoro/version"
require 'eventmachine'
+require "domodoro/version"
+require "domodoro/channel"
+require "domodoro/server"
+
module Domodoro
+ extend self
- def self.start(action, host, port)
+ def start(action, host, port)
case action
when 'serve', 'server'
Server.start(host, port)
@@ -12,67 +16,6 @@ def self.start(action, host, port)
end
end
- class Channel < EM::Channel
- def morning(min)
- if [0, 30].include?(min)
- self << 'START'
- end
- if [25, 55].include?(min)
- self << 'STOP'
- end
- end
-
- def afternoon(min)
- # if [20, 50].include?(min)
- # self << 'START'
- # end
- # if [45, 15].include?(min)
- # self << 'STOP'
- # end
- if (min % 2 == 0)
- self << 'START'
- else
- self << 'STOP'
- end
- end
- end
-
- class Server < EM::Connection
- class << self
- def start(host, port)
- EM.run do
- channel = Channel.new
-
- EM.start_server(host, port, self, channel)
-
- EM.add_periodic_timer(1) do
- if Time.now.sec == 0
- hour = Time.now.hour
- min = Time.now.min
- if (hour >= 8 && hour < 13)
- channel.morning(min)
- elsif (hour >= 13 && min >= 20) &&
- channel.afternoon(min)
- end
- end
- end
- end
- end
- end
-
- def initialize(channel)
- @channel = channel
- end
-
- def post_init
- @sid = @channel.subscribe { |m| send_data "#{m.inspect}\n" }
- end
-
- def unbind
- @channel.unsubscribe @sid
- end
- end
-
class Client
class << self
def start(host, port)
View
37 lib/domodoro/channel.rb
@@ -0,0 +1,37 @@
+module Domodoro
+ class Channel < EM::Channel
+ def broadcast(hour, min)
+ if ENV['DEBUG']
+ if min % 2 == 0
+ self << :start
+ else
+ self << :stop
+ end
+ return
+ end
+
+ if (hour >= 8 && hour < 13)
+ morning(min)
+ elsif (hour >= 13 && min >= 20) &&
+ afternoon(min)
+ end
+ end
+ def morning(min)
+ case min
+ when 0, 30
+ self << :start
+ when 25, 55
+ self << :stop
+ end
+ end
+
+ def afternoon(min)
+ case min
+ when 20, 50
+ self << :start
+ when 45, 15
+ self << :stop
+ end
+ end
+ end
+end
View
33 lib/domodoro/server.rb
@@ -0,0 +1,33 @@
+module Domodoro
+ class Server < EM::Connection
+ attr_reader :channel, :sid
+
+ class << self
+ def start(host, port)
+ EM.run do
+ channel = Channel.new
+
+ EM.start_server(host, port, self, channel)
+
+ EM.add_periodic_timer(1) do
+ if Time.now.sec == 0
+ channel.broadcast(Time.now.hour, Time.now.min)
+ end
+ end
+ end
+ end
+ end
+
+ def initialize(channel)
+ @channel = channel
+ end
+
+ def post_init
+ @sid = channel.subscribe { |m| send_data "#{m.inspect}\n" }
+ end
+
+ def unbind
+ channel.unsubscribe @sid
+ end
+ end
+end
View
133 test/domodoro/channel_test.rb
@@ -0,0 +1,133 @@
+require 'test_helper'
+
+module Domodoro
+ describe Channel do
+ before do
+ @channel = Channel.new
+ end
+
+ it 'is an EventMachine channel' do
+ assert @channel.is_a?(EM::Channel)
+ end
+
+ describe 'broadcast' do
+ describe 'in the morning' do
+ it 'broadcasts from 8:00 until 12:59' do
+ @channel.expects(:morning).with(5).never
+ @channel.expects(:morning).with(10).once
+ @channel.expects(:morning).with(15).once
+ @channel.expects(:morning).with(20).once
+ @channel.expects(:morning).with(25).once
+ @channel.expects(:morning).with(30).once
+ @channel.expects(:morning).with(35).never
+
+ min = 0
+ (7..13).each do |hour|
+ min += 5
+ @channel.broadcast(hour, min)
+ end
+ end
+ end
+
+ describe 'in the afternoon' do
+ it 'broadcasts from 13:20 on' do
+ @channel.expects(:afternoon).with(5).never
+ @channel.expects(:afternoon).with(10).never
+ @channel.expects(:afternoon).with(15).never
+ @channel.expects(:afternoon).with(20).once
+ @channel.expects(:afternoon).with(25).once
+ @channel.expects(:afternoon).with(30).once
+ @channel.expects(:afternoon).with(35).once
+
+ min = 0
+ (13..19).each do |hour|
+ min += 5
+ @channel.broadcast(hour, min)
+ end
+ end
+ end
+
+ it 'does not broadcast during lunch time' do
+ %w(morning afternoon).each do |timespan|
+ @channel.expects(timespan).with(5).never
+ end
+
+ (0..19).each do |min|
+ @channel.broadcast(13, min)
+ end
+ end
+ end
+
+ describe '#morning' do
+ describe 'pomodoro starts' do
+ it 'broadcasts a :start message when the time is XX:00' do
+ @channel.expects(:<<).with(:start)
+ @channel.morning 0
+ end
+
+ it 'broadcasts a :start message when the time is XX:30' do
+ @channel.expects(:<<).with(:start)
+ @channel.morning 30
+ end
+ end
+
+ describe 'pomodoro stops' do
+ it 'broadcasts a :stop message when the time is XX:25' do
+ @channel.expects(:<<).with(:stop)
+ @channel.morning 25
+ end
+
+ it 'broadcasts a :stop message when the time is XX:55' do
+ @channel.expects(:<<).with(:stop)
+ @channel.morning 55
+ end
+ end
+
+ it 'does not broadcast anything at other times' do
+ @channel.expects(:<<).never
+
+ @channel.morning 5
+ @channel.morning 10
+ @channel.morning 20
+ @channel.morning 40
+ @channel.morning 50
+ end
+ end
+
+ describe '#afternoon' do
+ describe 'pomodoro starts' do
+ it 'broadcasts a :start message when the time is XX:20' do
+ @channel.expects(:<<).with(:start)
+ @channel.afternoon 20
+ end
+
+ it 'broadcasts a :start message when the time is XX:50' do
+ @channel.expects(:<<).with(:start)
+ @channel.afternoon 50
+ end
+ end
+
+ describe 'pomodoro stops' do
+ it 'broadcasts a :stop message when the time is XX:45' do
+ @channel.expects(:<<).with(:stop)
+ @channel.afternoon 45
+ end
+
+ it 'broadcasts a :stop message when the time is XX:15' do
+ @channel.expects(:<<).with(:stop)
+ @channel.afternoon 15
+ end
+ end
+
+ it 'does not broadcast anything at other times' do
+ @channel.expects(:<<).never
+
+ @channel.afternoon 5
+ @channel.afternoon 10
+ @channel.afternoon 25
+ @channel.afternoon 40
+ @channel.afternoon 55
+ end
+ end
+ end
+end
View
65 test/domodoro/server_test.rb
@@ -0,0 +1,65 @@
+require 'test_helper'
+
+module Domodoro
+ describe Server do
+ include EM::MiniTest::Spec
+
+ before do
+ @channel = Channel.new
+ end
+
+ it 'initializes with a channel' do
+ server = Domodoro::Server.new(nil, @channel)
+ assert_equal @channel, server.channel
+ end
+
+ it 'repeats every message broadcasted to the channel' do
+ server = Server.new(nil, @channel)
+ EM.start_server('127.0.0.1', 8888, Server, :app => server)
+
+ server.expects(:send_data).with(":start\n")
+
+ @channel << :start
+ end
+
+ describe '.start' do
+ it 'opens a server with a new channel' do
+ Channel.expects(:new).returns @channel
+ EM.expects(:start_server).with('0.0.0.0', '8888', Server, @channel)
+
+ Server.start('0.0.0.0', '8888')
+ end
+
+ it 'adds a timer by second that calls broadcast every minute', :timeout => 4 do
+ Channel.expects(:new).returns(@channel).at_least(1)
+ Server.start('0.0.0.0', '8888')
+
+ module Tick
+ attr_accessor :num
+ extend self
+ end
+
+ Tick.num = 0
+
+ now = stub
+ Time.expects(:now).at_least(3).returns now
+ now.stubs(:hour).returns 0
+ now.stubs(:min).returns 0
+ now.stubs(:sec).returns 0
+
+ def @channel.broadcast(*args)
+ puts 'tick'
+ Tick.num += 1
+ end
+
+ EM.add_timer(3.5) do
+ assert_equal 3, Tick.num
+ done!
+ end
+
+ wait!
+ end
+ end
+
+ end
+end
View
25 test/domodoro_test.rb
@@ -0,0 +1,25 @@
+require 'test_helper'
+
+describe Domodoro do
+ describe '.start' do
+
+ describe 'as a server' do
+ it 'spawns a Domodoro server' do
+ host, port = stub, stub
+ Domodoro::Server.expects(:start).with(host, port)
+
+ Domodoro.start 'serve', host, port
+ end
+ end
+
+ describe 'as a client' do
+ it 'starts a Domodoro client' do
+ host, port = stub, stub
+ Domodoro::Client.expects(:start).with(host, port)
+
+ Domodoro.start 'join', host, port
+ end
+ end
+
+ end
+end
View
13 test/hey_test.rb
@@ -1,13 +0,0 @@
-require 'test_helper'
-
-describe 'test' do
- include EM::MiniTest::Spec
-
- it 'passes' do
- assert 3
- assert 3
- assert 3
- assert 3
- assert 3
- end
-end
View
7 test/support/em-minitest.rb
@@ -79,11 +79,16 @@ module ClassMethods # :nodoc:
def it *args, &block # :nodoc:
return super unless block_given?
+ timeout = 0.1
+ if args.last.is_a?(Hash) && args.last[:timeout]
+ timeout = args.pop[:timeout]
+ end
+
super do
@wait = false
EM.run do
- @timeout = EM.add_timer(0.1) do
+ @timeout = EM.add_timer(timeout) do
flunk "test timed out!"
end
View
12 test/test_helper.rb
@@ -1,11 +1,23 @@
require 'rubygems'
gem 'minitest'
+require 'mocha'
require 'minitest/spec'
+require 'purdytest'
require 'minitest/autorun'
Dir['test/support/*.rb'].each do |file|
require file
end
require 'domodoro'
+
+class MiniTest::Unit::TestCase
+ include Mocha::API
+ def setup
+ mocha_setup
+ end
+ def teardown
+ mocha_teardown
+ end
+end

0 comments on commit 9c217d1

Please sign in to comment.