Same mailer instance being returned regardless of which request is passed to get_mailer

9 views
Skip to first unread message

pyramidX

unread,
Dec 22, 2014, 3:04:27 PM12/22/14
to pylons-...@googlegroups.com
In a given unit test function I create 2 requests, each send one email. Even though I'm passing 2 different instances of DummyRequest to get_mailer, the DummyMailer objects returned are the same instance. You can see in my last assert statement it fails because there are 2 emails in the outbox (one created by request1 and the other by request2).

I can do a mailer1.outbox.clear() to get the expected behavior. But is this the right way of doing this? How come get_mailer behaves like this, returning the same instance every time?

request1 = testing.DummyRequest(...)
views
.send_one_email(...)
mailer1
= get_mailer(request1)

# mailer1.outbox.clear()  # Why is this necessary for the last assert below not to fail?

request2
= testing.DummyRequest(...)
views
.send_one_different_email(...)
mailer2
= get_mailer(request2)

assert request1 is not request2  # Not the same request instances
assert mailer1 is mailer2  # It's the same mailer instance
assert len(mailer1.outbox) == 1
assert len(mailer2.outbox) == 1  # Fails

It works the same if I do it like this, which wouldn't make me think there will be 2 different mailers returned, but in this case what's the purpose of passing a DummyRequest to get_mailer?

single_mailer = get_mailer(testing.DummyRequest())

request1 = testing.DummyRequest(...)
views
.send_one_email(...)

request2
= testing.DummyRequest(...)
views
.send_one_different_email(...)

assert request1 is not request2  # Not the same request instances

assert len(single_mailer.outbox) == 1
assert len(single_mailer.outbox) == 1  # Fails

Tres Seaver

unread,
Dec 22, 2014, 3:40:11 PM12/22/14
to pylons-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 12/22/2014 03:04 PM, pyramidX wrote:
> It works the same if I do it like this, which wouldn't make me think
> there will be 2 different mailers returned, but in this case what's
> the purpose of passing a DummyRequest to get_mailer?

The mailer is a "utility" (a singleton), registered in 'request.registry'.


Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 tse...@palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlSYgR4ACgkQ+gerLs4ltQ7B7gCg0vedXKY8kRhkjHRLORFY24+U
01AAn2y++SpE+ov7FeYjc0N9mEieL1js
=0v4E
-----END PGP SIGNATURE-----

Michael Merickel

unread,
Dec 22, 2014, 3:43:06 PM12/22/14
to Pylons
The mailer is a utility that represents an external service. It is not
different per-request just like how your smtp server is not different
per email you send.

Using .clear() is appropriate here.
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.

Jonathan Vanasco

unread,
Dec 22, 2014, 3:44:22 PM12/22/14
to pylons-...@googlegroups.com, tse...@palladion.com


On Monday, December 22, 2014 3:40:11 PM UTC-5, Tres Seaver wrote:

The mailer is a "utility" (a singleton), registered in 'request.registry'.

Just to expand on this answer a bit...

The mailer is designed to be configured-for and associated-with the scope of an application -- not each individual request. 

pyramidX

unread,
Dec 22, 2014, 4:28:06 PM12/22/14
to pylons-...@googlegroups.com, tse...@palladion.com
I see, I'm accessing it now with

mailer = config.registry.getUtility(IMailer)

which definitely gives it more of an utility kind of feel. Thanks!
Reply all
Reply to author
Forward
0 new messages