Bug in YABE sample?

7 views
Skip to first unread message

johncch

unread,
Mar 18, 2010, 3:40:02 AM3/18/10
to play-framework
Dear all,

First of all, I'd like to say I'm enjoying Play from what I've seen so
far.

I'm meeting something.. quite weird I'd say and I'd like to ask if
this is a bug. It happened when I was going through the yabe tutorial
and I verified it against the play 1.0.1.

It has something to do with the unit test, in the fullTest() method of
the BasicTest.java

<code>
// Find the most recent post
Post frontPost = Post.find("order by postedAt
desc").first();
assertNotNull(frontPost);
assertEquals("About the model layer", frontPost.title);

// Check that this post has two comments
155 //assertEquals(2, frontPost.comments.size());

// Post a new comment
frontPost.addComment("Jim", "Hello guys");
159 assertEquals(3, frontPost.comments.size());
assertEquals(4, Comment.count());
</code>

If I remove line 155 and run the unit test, I get:

Failure, expected:<3> but was:<4>
In /test/BasicTest.java, line 159 :
assertEquals(3, frontPost.comments.size());

If I add back line 155, or even wrap it in System.out, it's fine. It's
interesting in the quantum mechanical sense that If I don't read the
value, the error will always be there, but if I read it, then the test
would pass.

I'm still new to the framework so I'm wondering if there's something
I'm missing or it's a.. bug.

Thanks!

Guillaume Bort

unread,
Mar 18, 2010, 5:34:45 AM3/18/10
to play-fr...@googlegroups.com
Yes it's strange. I'll investiguate.

> --
> You received this message because you are subscribed to the Google
> Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en
> .
>

Reeves

unread,
Mar 18, 2010, 6:47:08 AM3/18/10
to play-framework
I just looked at addComment method in Post

public Post addComment(String author, String content) {
43: Comment newComment = new Comment(this, author,
content).save();
44 this.comments.add(newComment);
}

lNo 43 --> we created comment and saved it.
lNo 44 --> we created an association --> we dont need to save it here?

if you add new line 45: this.save() --> everything should be fine.

Don't ask me why yet :) I am just trying to find out how play wraps
the code with transactions.

cheers


On Mar 18, 9:34 am, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Yes it's strange. I'll investiguate.
>

Guillaume Bort

unread,
Mar 19, 2010, 6:36:24 AM3/19/10
to play-fr...@googlegroups.com
Yes you're right, it was the problem.

> It's interesting in the quantum mechanical sense that If I don't read the
> value, the error will always be there, but if I read it, then the test
> would pass.

In fact it is not as simple. When you call "frontPost.comments.size()"
hibernate will generate a "select count ..." query. But before
generating this query, it will take care of flushing all not yet saved
object states to the database.

So yes here reading a value on a persistent object can have side
effect when running in the same database transaction.

John Wells

unread,
Mar 19, 2010, 7:56:46 AM3/19/10
to play-fr...@googlegroups.com
On Fri, Mar 19, 2010 at 6:36 AM, Guillaume Bort <guillau...@gmail.com> wrote:
Yes you're right, it was the problem.

> It's interesting in the quantum mechanical sense that If I don't read the
> value, the error will always be there, but if I read it, then the test
> would pass.

In fact it is not as simple. When you call "frontPost.comments.size()"
hibernate will generate a "select count ..." query. But before
generating this query, it will take care of flushing all not yet saved
object states to the database.

So yes here reading a value on a persistent object can have side
effect when running in the same database transaction.


So will this continue to be the case, or is there a fix? 

Reeves

unread,
Mar 22, 2010, 1:08:22 PM3/22/10
to play-framework
In actual application flow, after adding a comment we redirect it to
comments listing page / blog detail page, so the transaction is
closed.
But in test environment that is not the case hmm so, each test method
is wrapped with one transaction? Even if it is a single transaction
hibernate should handle it. isn't it?
Can somebody explain me if this is a bug in play framework or bug in
yabe app.

cheers

Guillaume Bort

unread,
Mar 22, 2010, 3:36:58 PM3/22/10
to play-fr...@googlegroups.com
Well, that's right that when you run a JUnit test cases, transactions
are managed differently (only one transaction is used to run to
complete test class). By the way it was a bug in the YABE application
that I have fixed in the trunk.

johncch

unread,
Mar 23, 2010, 4:15:25 AM3/23/10
to play-framework
Thank you for checking it out. I'll check out the project when I have
time.

Good job!

Reeves

unread,
Mar 23, 2010, 5:40:02 AM3/23/10
to play-framework
Thanks Guillaume
Reply all
Reply to author
Forward
0 new messages