def record_thread_locale(record, event, locale): """Set and capture locale in current thread. Use an event to coordinate execution for multithreaded tests. Because thread idents maybe recycled and reused and jdatetime uses threads idents to identify unique threads. """ event.wait(timeout=10) jdatetime.set_locale(locale) record.append(jdatetime.get_locale())
def record_thread_locale(record, event, locale): """Set and capture locale in current thread. Use an event to coordinate execution for multithreaded tests. Because thread idents maybe recycled and reused and jdatetime uses threads idents to identify unique threads. """ event.wait(timeout=10) jdatetime.set_locale(locale) record.append(jdatetime.get_locale())
def test_set_locale_is_per_thread_with_no_effect_on_other_threads(self): event = threading.Event() fa_record = [] fa_thread = threading.Thread(target=self.record_thread_locale, args=(fa_record, event, 'fa_IR')) nl_record = [] nl_thread = threading.Thread(target=self.record_thread_locale, args=(nl_record, event, 'nl_NL')) fa_thread.start() nl_thread.start() event.set() # ensure both threads run concurrently fa_thread.join() nl_thread.join() self.assertEqual(1, len(fa_record)) self.assertEqual('fa_IR', fa_record[0]) self.assertEqual(1, len(nl_record)) self.assertEqual('nl_NL', nl_record[0]) self.assertIsNone(jdatetime.get_locale()) # MainThread is not affected neither
def test_set_locale_is_per_thread_with_no_effect_on_other_threads(self): event = threading.Event() fa_record = [] fa_thread = threading.Thread(target=self.record_thread_locale, args=(fa_record, event, 'fa_IR')) nl_record = [] nl_thread = threading.Thread(target=self.record_thread_locale, args=(nl_record, event, 'nl_NL')) fa_thread.start() nl_thread.start() event.set() # ensure both threads run concurrently fa_thread.join() nl_thread.join() self.assertEqual(1, len(fa_record)) self.assertEqual('fa_IR', fa_record[0]) self.assertEqual(1, len(nl_record)) self.assertEqual('nl_NL', nl_record[0]) self.assertIsNone( jdatetime.get_locale()) # MainThread is not affected neither
def record_greenlet_locale_nl(): jdatetime.set_locale('nl_NL') fa_greenlet.switch() nl_record.append(jdatetime.get_locale()) fa_greenlet.switch()
def record_greenlet_locale_fa(): jdatetime.set_locale('fa_IR') nl_greenlet.switch() fa_record.append(jdatetime.get_locale()) nl_greenlet.switch()
def test_get_locale_returns_none_if_no_locale_set_yet(self): self.assertIsNone(jdatetime.get_locale())
def test_init_locale_is_effective_only_if_not_empty(self): orig_locale = jdatetime.get_locale() jdatetime.set_locale('nl_NL') self.addCleanup(jdatetime.set_locale, orig_locale) date = jdatetime.date(1397, 4, 22, locale='') self.assertEqual(date.locale, 'nl_NL')
def test_init_locale_is_effective_only_if_not_none(self): orig_locale = jdatetime.get_locale() jdatetime.set_locale('en_US') self.addCleanup(jdatetime.set_locale, orig_locale) date = jdatetime.date(1397, 4, 22, locale=None) self.assertEqual(date.locale, 'en_US')
def record_greenlet_locale_nl(): jdatetime.set_locale('nl_NL') fa_greenlet.switch() nl_record.append(jdatetime.get_locale()) fa_greenlet.switch()
def record_greenlet_locale_fa(): jdatetime.set_locale('fa_IR') nl_greenlet.switch() fa_record.append(jdatetime.get_locale()) nl_greenlet.switch()
def test_get_locale_returns_none_if_no_locale_set_yet(self): self.assertIsNone(jdatetime.get_locale())
def test_init_locale_is_effective_only_if_not_empty(self): orig_locale = jdatetime.get_locale() jdatetime.set_locale('nl_NL') self.addCleanup(jdatetime.set_locale, orig_locale) date = jdatetime.date(1397, 4, 22, locale='') self.assertEqual(date.locale, 'nl_NL')
def test_init_locale_is_effective_only_if_not_none(self): orig_locale = jdatetime.get_locale() jdatetime.set_locale('en_US') self.addCleanup(jdatetime.set_locale, orig_locale) date = jdatetime.date(1397, 4, 22, locale=None) self.assertEqual(date.locale, 'en_US')