Skip to content

Conversation

@iamrichardjones
Copy link
Contributor

Add additional unit tests to show that singletons can be created in single thread environment and multithread environment. Also add a test to demonstrate a whole with Singleton when instantiating using reflection

(first ever pull request so if ive done something wrong or broken etiquette then apologies and let me know - thanks)

Richard Jones added 2 commits October 11, 2015 21:32
…ingle thread environment and multithread environment. Also add a test to demonstrate a whole with Singleton when instantiating using reflection
…ingle thread environment and multithread environment. Also add a test to demonstrate a whole with Singleton when instantiating using reflection

Add some logging. Tests pass locally but not on github?
@iamrichardjones
Copy link
Contributor Author

adding some logging on my next commit. I cannot see where the error is because the tests pass locally

@iamrichardjones iamrichardjones changed the title Update to singleton unit tests Update to singleton unit tests and lazy loading unit test Oct 12, 2015
@iluwatar
Copy link
Owner

There seems to be intermittent failures in LazyLoadedSingletonThreadSafetyTest#test_MultipleCallsReturnTheSameObjectInDifferentThreads. Can you repeat this? Here is the stack trace:

java.lang.AssertionError: expected:<null> but was:<com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower@14514713>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest.test_MultipleCallsReturnTheSameObjectInDifferentThreads(LazyLoadedSingletonThreadSafetyTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

@iamrichardjones
Copy link
Contributor Author

Hi iluwatar, Yes I can reproduce it. I put the test in a loop of 2000 iterations and I got it after a couple of runs. i'll investigate. Thanks

@iamrichardjones
Copy link
Contributor Author

Hi iluwatar, I have run the test in a loop of 5000 and run this about 20 times. No error now. Thanks for spotting. I had to use a syncrhonized list as it was being manipulated by multiple threads. I pushed the code about 30 minutes ago. The build seems to ahve failed on coverage/coveralls. I only added the syncrhonised bit to the list. What do I need to do to get this working. Sorry im new to GitHub.
Thanks

@iluwatar
Copy link
Owner

Good job @iamrichardjones It seems to work now.

iluwatar added a commit that referenced this pull request Oct 19, 2015
Update to singleton unit tests and lazy loading unit test
@iluwatar iluwatar merged commit 8eff279 into iluwatar:master Oct 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants