# Vaguely humorous Test::Unit problem (long)

**URL:** <https://www.ruby-forum.com/t/vaguely-humorous-test-unit-problem-long/107784>\
**Category:** Ruby\
**Created:** [June 26, 2007, 8:40am UTC](https://www.ruby-forum.com/t/vaguely-humorous-test-unit-problem-long/107784 "2007-06-26T08:40:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 26, 2007, 8:40am UTC](https://www.ruby-forum.com/t/vaguely-humorous-test-unit-problem-long/107784/1 "2007-06-26T08:40:43Z")

</div>

Hi everybody,

I have a problem I can’t figure out. I’ve created a test class using  
Test::Unit::TestCase. One of the test cases is failing, but if I  
comment out the passing one, the failing one passes. Moreover, if I  
rename the failing test to something preceeding the passing one in  
alphabetical order, it passes.

The issue is a bit complicated, however. I’ll post some code, then  
explain it.

require ‘drb’  
require ‘test/unit’  
require ‘…/lib/classes/Config.rb’  
require ‘…/lib/classes/Service.rb’

class TestService \< Test::Unit::TestCase

def test\_statistics # fails, unless named e.g. test\_astatistics  
message = {:data =\> [1,1,1], :mixin =\> :statistics}.to\_config  
config = {:url =\> ‘druby://localhost:9001’,}.to\_config

```
remote_service = Service.new(config)
DRb.start_service()
service = DRbObject.new(nil, config.url)
assert_equal([:statistics, 1, 0], service.process(message))

```

end

def test\_service\_drb # Always passes.  
message = {:data =\> 1, :mixin =\> :trivial}.to\_config  
config = {:url =\> ‘druby://localhost:9000’,}.to\_config

```
remote_service = Service.new(config)
DRb.start_service()
service = DRbObject.new(nil, config.url)
assert_equal(service.process(message),1)

```

end  
end

EOC

Note that outside of the message and config objects, and parameters to  
assert\_equal, the code is identical.

The Hash.to\_config method turns hash keys into attr\_reader’s and hash  
values into the reader’s return value. The new object’s class is  
Config.

Oh, right, I should explain what a Service object is. First off,  
Service.new(config) takes in a Config object. It also starts a DRb  
server with the line:

DRb.start\_service(@config.url, self)

service.process is harder to explain. Essentially, Service objects  
are servers that dynamically change how they process data depending on  
the type of data they recieve. I implemented this idea using mixins  
and a stable public API. So for each call of  
service.process(message), the Service object checks the value of  
config.mixin, compares it to the currently loaded module, and requires  
and includes it if they differ. Then it calls the run(message.data)  
method, which is implemented by the mixed in module. The Service  
class:

class Service

def initialize(config)  
@mixin = “”  
@config = config  
DRb.start\_service(@config.url, self)  
end

def process(message)  
new\_mixin = message.mixin  
unless @mixin == new\_mixin  
require “…/lib/services/#{new\_mixin.to\_s.capitalize}.rb”  
self.class.class\_eval(“include #{new\_mixin.to\_s.capitalize}” )  
@mixin = new\_mixin  
end  
self.run(message.data)  
end  
end

EOC

Little help?  
Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 26, 2007, 9:58am UTC](https://www.ruby-forum.com/t/vaguely-humorous-test-unit-problem-long/107784/2 "2007-06-26T09:58:50Z")

</div>

You’re calling DRb.start\_service a lot. In my experience, this can lead  
to  
problems. Try adding DRb.stop\_service in #teardown.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 26, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/vaguely-humorous-test-unit-problem-long/107784/3 "2007-06-26T22:26:33Z")

</div>

On Jun 26, 1:13 pm, removed\_email\_address@domain.invalid wrote:

> def test\_service\_stat # fails  
> end  
> Statistics::run(data). Is DRb threaded? Could this be a race  
> condition in DRb and not my code? (I can’t see how it would by a bug  
> in my code)

My mistake. DRb is obviously threaded. I meant to ask if Test::Unit  
is threaded, and if there might be a race condition there.

Alex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 26, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/vaguely-humorous-test-unit-problem-long/107784/4 "2007-06-26T22:16:31Z")

</div>

On Jun 26, 12:57 am, Sylvain J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## You’re calling DRb.start\_service a lot. In my experience, this can lead to problems. Try adding DRb.stop\_service in #teardown.
> 
> Sylvain J.

Well, I refactored the tests so that Drb.start\_service is only called  
once. I still get the name dependent failure.

require ‘drb’  
require ‘test/unit’  
require ‘…/lib/classes/Config.rb’  
require ‘…/lib/classes/Service.rb’

class TestService \< Test::Unit::TestCase

@@config = {:url =\> ‘druby://localhost:9001’,}.to\_config  
@@remote\_service = Service.new(@@config) # calls DRb.service\_start

def test\_service\_stat # fails  
message = {:data =\> [1,1,1], :mixin =\> :statistics}.to\_config  
service = DRbObject.new(nil, @@config.url)  
assert\_equal([:statistics, 1, 0], service.process(message))  
end

def test\_service\_trivial  
message = {:data =\> 1, :mixin =\> :trivial}.to\_config  
service = DRbObject.new(nil, @@config.url)  
assert\_equal(service.process(message),1)  
end  
end

EOC

One thing I forgot to mention is that test\_service\_statistics fails  
with:

\<[:statistics, 1, 0]\> expected but was  
\<[1, 1, 1]\>

which means that it was actually using Trivial::run(data) instead of  
Statistics::run(data). Is DRb threaded? Could this be a race  
condition in DRb and not my code? (I can’t see how it would by a bug  
in my code)

Thanks,  
Alex
