Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added a few unit tests for threads and fibers

  • Loading branch information...
commit acd4a7fa20e76ce8888e19f5f35ac73672e79a90 1 parent 3a666fe
Sean Kelly complexmath authored

Showing 1 changed file with 70 additions and 0 deletions. Show diff stats Hide diff stats

  1. +70 0 src/core/thread.d
70 src/core/thread.d
@@ -1770,6 +1770,41 @@ else
1770 1770 static assert(0, "Platform not supported.");
1771 1771 }
1772 1772
  1773 +
  1774 +unittest
  1775 +{
  1776 + int x = 0;
  1777 +
  1778 + auto t = new Thread(
  1779 + {
  1780 + x++;
  1781 + });
  1782 + t.start(); t.join();
  1783 + assert( x == 1 );
  1784 +}
  1785 +
  1786 +
  1787 +unittest
  1788 +{
  1789 + enum MSG = "Test message.";
  1790 + string caughtMsg;
  1791 +
  1792 + try
  1793 + {
  1794 + auto t = new Thread(
  1795 + {
  1796 + throw new Exception( MSG );
  1797 + });
  1798 + t.start(); t.join();
  1799 + assert( false, "Expected rethrown exception." );
  1800 + }
  1801 + catch( Throwable t )
  1802 + {
  1803 + assert( t.msg == MSG );
  1804 + }
  1805 +}
  1806 +
  1807 +
1773 1808 ///////////////////////////////////////////////////////////////////////////////
1774 1809 // GC Support Routines
1775 1810 ///////////////////////////////////////////////////////////////////////////////
@@ -4121,12 +4156,14 @@ version( unittest )
4121 4156 }
4122 4157 }
4123 4158
  4159 +
4124 4160 // Single thread running separate fibers
4125 4161 unittest
4126 4162 {
4127 4163 runTen();
4128 4164 }
4129 4165
  4166 +
4130 4167 // Multiple threads running separate fibers
4131 4168 unittest
4132 4169 {
@@ -4138,6 +4175,7 @@ unittest
4138 4175 group.joinAll();
4139 4176 }
4140 4177
  4178 +
4141 4179 // Multiple threads running shared fibers
4142 4180 unittest
4143 4181 {
@@ -4186,6 +4224,7 @@ unittest
4186 4224 }
4187 4225 }
4188 4226
  4227 +
4189 4228 // Test exception handling inside fibers.
4190 4229 unittest
4191 4230 {
@@ -4204,6 +4243,36 @@ unittest
4204 4243 assert(caughtMsg == MSG);
4205 4244 }
4206 4245
  4246 +
  4247 +unittest
  4248 +{
  4249 + int x = 0;
  4250 +
  4251 + (new Fiber({
  4252 + x++;
  4253 + })).call();
  4254 + assert( x == 1 );
  4255 +}
  4256 +
  4257 +
  4258 +unittest
  4259 +{
  4260 + enum MSG = "Test message.";
  4261 +
  4262 + try
  4263 + {
  4264 + (new Fiber({
  4265 + throw new Exception( MSG );
  4266 + })).call();
  4267 + assert( false, "Expected rethrown exception." );
  4268 + }
  4269 + catch( Throwable t )
  4270 + {
  4271 + assert( t.msg == MSG );
  4272 + }
  4273 +}
  4274 +
  4275 +
4207 4276 version( AsmX86_64_Posix )
4208 4277 {
4209 4278 unittest
@@ -4223,6 +4292,7 @@ version( AsmX86_64_Posix )
4223 4292 }
4224 4293 }
4225 4294
  4295 +
4226 4296 version( OSX )
4227 4297 {
4228 4298 // NOTE: The Mach-O object file format does not allow for thread local

0 comments on commit acd4a7f

David Nadlinger

Won't this also catch the AssertError in non-release mode? (The exception message check will fail, sure, but why don't just catch Exception?)

Jonathan M Davis

Yes it will. You should basically never catch Throwable. Ideally, assertThrown would be used here, but that's in Phobos (though a copy could be made - IIRC core.time does that). Barring that, a bool could be used to indicate whether the end of the try was reached, and its value could be asserted after the catch to verify that the end of the try wasn't reached.

Sean Kelly

Good point. I think the result will be the same in this case, given how the test is written, but I'll change it to Exception anyway.

Please sign in to comment.
Something went wrong with that request. Please try again.