-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
194 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# import os | ||
import time | ||
# from flask_mailgun import async | ||
from multiprocessing import Pool # , active_children | ||
from functools import wraps | ||
import unittest | ||
|
||
|
||
# function decorator | ||
def async_pool(pool_size): | ||
|
||
def wrapper(func): | ||
pool = Pool(pool_size) | ||
|
||
@wraps(func) | ||
def inner(*args, **kwargs): | ||
return pool.apply_async(func, args=args, kwds=kwargs) | ||
return inner | ||
return wrapper | ||
|
||
|
||
def runner(fun): | ||
results = [fun(i) for i in xrange(20)] | ||
for result in results: | ||
result.wait() | ||
result.get() | ||
|
||
|
||
def foo(arg): | ||
time.sleep(0.1) | ||
return arg | ||
|
||
|
||
class AsyncTest(unittest.TestCase): | ||
|
||
def test_async(self): | ||
async_foo = async_pool(4)(foo) | ||
runner(async_foo) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Thu Jun 2 12:08:14 2016 | ||
@author: richard | ||
""" | ||
import unittest | ||
import os | ||
import shutil | ||
import tempfile | ||
from werkzeug import FileStorage | ||
from tests.fixtures import get_attachment | ||
from test_flask_mailgun import MailgunTestBase | ||
|
||
|
||
class SaveAttachmentTest(MailgunTestBase): | ||
def setUp(self): | ||
super(SaveAttachmentTest, self).setUp() | ||
(filename, file_stream) = get_attachment() | ||
self.attachment = FileStorage(stream=file_stream, | ||
filename=filename) | ||
|
||
def tearDown(self): | ||
super(SaveAttachmentTest, self).tearDown() | ||
self.attachment.close() | ||
|
||
def test_fileallowed(self): | ||
self.assertTrue(self.mailgun.file_allowed('test.txt')) | ||
self.assertFalse(self.mailgun.file_allowed('bob')) | ||
|
||
def test_save_attachments(self): | ||
testdir = tempfile.mkdtemp() | ||
self.attachment.seek(0) | ||
filenames = self.mailgun.save_attachments([self.attachment], testdir) | ||
filenames = [os.path.basename(filename) for filename in filenames] | ||
self.assertTrue(set(os.listdir(testdir)) == set(filenames)) | ||
self.assertEqual(len(os.listdir(testdir)), 1) | ||
shutil.rmtree(testdir) | ||
with self.assertRaises(OSError): | ||
os.listdir(testdir) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters