{"action":"created","comment":{"url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/comments/53564114","id":53564114,"diff_hunk":"@@ -0,0 +1,121 @@\n+import org.junit.Test;\n+import static org.junit.Assert.*;\n+\n+import java.util.concurrent.CyclicBarrier;\n+import java.util.function.Supplier;\n+\n+public class LazyFactoryTest {\n+\n+    public class CountedSupplier<T> implements Supplier<T> {\n+\n+        private int count = 0;\n+        private final Supplier<T> sup;\n+\n+        CountedSupplier(Supplier<T> sup) {\n+            this.sup = sup;\n+        }\n+\n+        public int getCount() {\n+            return  count;\n+        }\n+\n+        public T get() {\n+            count++;\n+            return sup.get();\n+        }\n+    }\n+\n+    class SingleThreadTester<T> {\n+        private final Supplier<T> supplier;\n+        private final T expected;\n+\n+        public SingleThreadTester(Supplier<T> supplier, T expected) {\n+            this.supplier = supplier;\n+            this.expected = expected;\n+        }\n+\n+        public void run() {\n+            CountedSupplier<T> countedSupplier = new CountedSupplier<>(supplier);\n+            Lazy<T> lazy = LazyFactory.createLazy(countedSupplier);\n+\n+            assertEquals(0, countedSupplier.getCount());\n+            assertEquals(expected, lazy.get());\n+            assertEquals(1, countedSupplier.getCount());\n+\n+            lazy.get();\n+            lazy.get();\n+\n+            assertEquals(1, countedSupplier.getCount());\n+        }\n+    }\n+\n+    @Test\n+    public void testLazySingleThread() {\n+        new SingleThreadTester<>(() -> \"abc\", \"abc\").run();\n+        new SingleThreadTester<>(() -> null, null).run();\n+    }\n+\n+    class MultiThreadTester<T> {\n+        private final Lazy<T> lazy;\n+        private final T expected;\n+        private final int threadCount;\n+\n+\n+        public MultiThreadTester(Lazy<T> lazy, T expected, int threadCount) {\n+            this.lazy = lazy;\n+            this.expected = expected;\n+            this.threadCount = threadCount;\n+        }\n+\n+        public void run(){\n+            CyclicBarrier barrier = new CyclicBarrier(threadCount);\n+            Thread[] threads = new Thread[threadCount];\n+\n+            Object[] answers = new Object[threadCount];\n+            for (int i = 0; i < threadCount; i++) {\n+                final int t = i;\n+                threads[t] = new Thread(() -> {\n+                    try {\n+                        barrier.await();\n+                    } catch (Exception e) {\n+                        e.printStackTrace();\n+                    }\n+                    answers[t] = lazy.get();\n+                    assertEquals(expected, lazy.get());\n+                });\n+                threads[t].start();\n+            }\n+            for (int i = 0; i < threadCount; i++) {\n+                try {\n+                    threads[i].join();\n+                } catch (Exception e) {\n+                    e.printStackTrace();\n+                }\n+            }\n+            for (int i = 1; i < threadCount; i++) {\n+                assertSame(answers[i - 1], answers[i]);\n+            }\n+        }\n+    }\n+\n+    @Test\n+    public void testLazyMultiThread() {\n+        Supplier<String> sup = () -> \"abc\";\n+        CountedSupplier<String> countedSup = new CountedSupplier<>(sup);\n+        Lazy<String> lazyStr = LazyFactory.createLazyMT(countedSup);\n+\n+        new MultiThreadTester<>(lazyStr, \"abc\",  20).run();\n+\n+        assertEquals(1, countedSup.getCount());\n+    }\n+\n+\n+    @Test\n+    public void testLazyMultiThreadLockFree() {\n+        Supplier<String> sup = () -> \"abc\";\n+        Lazy<String> lazyStr = LazyFactory.createLazyLockFree(sup);\n+\n+        new MultiThreadTester<>(lazyStr, \"abc\", 20).run();\n+    }\n+","path":"task1/src/test/java/LazyFactoryTest.java","position":120,"original_position":120,"commit_id":"721ae6b9724f0fbf1f227b5101fc43e281d1406d","original_commit_id":"721ae6b9724f0fbf1f227b5101fc43e281d1406d","user":{"login":"sproshev","id":4990763,"avatar_url":"https://avatars.githubusercontent.com/u/4990763?v=3","gravatar_id":"","url":"https://api.github.com/users/sproshev","html_url":"https://github.com/sproshev","followers_url":"https://api.github.com/users/sproshev/followers","following_url":"https://api.github.com/users/sproshev/following{/other_user}","gists_url":"https://api.github.com/users/sproshev/gists{/gist_id}","starred_url":"https://api.github.com/users/sproshev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sproshev/subscriptions","organizations_url":"https://api.github.com/users/sproshev/orgs","repos_url":"https://api.github.com/users/sproshev/repos","events_url":"https://api.github.com/users/sproshev/events{/privacy}","received_events_url":"https://api.github.com/users/sproshev/received_events","type":"User","site_admin":false},"body":"В многопоточных тестах тоже надо проверить, что вычисление происходит только по требованию.","created_at":"2016-02-21T09:35:40Z","updated_at":"2016-02-21T09:35:40Z","html_url":"https://github.com/SashaMalysheva/java-course-hw/pull/1#discussion_r53564114","pull_request_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/comments/53564114"},"html":{"href":"https://github.com/SashaMalysheva/java-course-hw/pull/1#discussion_r53564114"},"pull_request":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1","id":59292504,"html_url":"https://github.com/SashaMalysheva/java-course-hw/pull/1","diff_url":"https://github.com/SashaMalysheva/java-course-hw/pull/1.diff","patch_url":"https://github.com/SashaMalysheva/java-course-hw/pull/1.patch","issue_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/1","number":1,"state":"open","locked":false,"title":"Java02. ДЗ 01, Малышева Александра, подгруппа 2","user":{"login":"SashaMalysheva","id":11505069,"avatar_url":"https://avatars.githubusercontent.com/u/11505069?v=3","gravatar_id":"","url":"https://api.github.com/users/SashaMalysheva","html_url":"https://github.com/SashaMalysheva","followers_url":"https://api.github.com/users/SashaMalysheva/followers","following_url":"https://api.github.com/users/SashaMalysheva/following{/other_user}","gists_url":"https://api.github.com/users/SashaMalysheva/gists{/gist_id}","starred_url":"https://api.github.com/users/SashaMalysheva/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SashaMalysheva/subscriptions","organizations_url":"https://api.github.com/users/SashaMalysheva/orgs","repos_url":"https://api.github.com/users/SashaMalysheva/repos","events_url":"https://api.github.com/users/SashaMalysheva/events{/privacy}","received_events_url":"https://api.github.com/users/SashaMalysheva/received_events","type":"User","site_admin":false},"body":"@sproshev","created_at":"2016-02-14T10:43:40Z","updated_at":"2016-02-21T09:35:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ff7e3a2a16f6b94111b1c955edf09f8ffa4714da","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1/commits","review_comments_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1/comments","review_comment_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/1/comments","statuses_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/statuses/721ae6b9724f0fbf1f227b5101fc43e281d1406d","head":{"label":"SashaMalysheva:task1","ref":"task1","sha":"721ae6b9724f0fbf1f227b5101fc43e281d1406d","user":{"login":"SashaMalysheva","id":11505069,"avatar_url":"https://avatars.githubusercontent.com/u/11505069?v=3","gravatar_id":"","url":"https://api.github.com/users/SashaMalysheva","html_url":"https://github.com/SashaMalysheva","followers_url":"https://api.github.com/users/SashaMalysheva/followers","following_url":"https://api.github.com/users/SashaMalysheva/following{/other_user}","gists_url":"https://api.github.com/users/SashaMalysheva/gists{/gist_id}","starred_url":"https://api.github.com/users/SashaMalysheva/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SashaMalysheva/subscriptions","organizations_url":"https://api.github.com/users/SashaMalysheva/orgs","repos_url":"https://api.github.com/users/SashaMalysheva/repos","events_url":"https://api.github.com/users/SashaMalysheva/events{/privacy}","received_events_url":"https://api.github.com/users/SashaMalysheva/received_events","type":"User","site_admin":false},"repo":{"id":51680777,"name":"java-course-hw","full_name":"SashaMalysheva/java-course-hw","owner":{"login":"SashaMalysheva","id":11505069,"avatar_url":"https://avatars.githubusercontent.com/u/11505069?v=3","gravatar_id":"","url":"https://api.github.com/users/SashaMalysheva","html_url":"https://github.com/SashaMalysheva","followers_url":"https://api.github.com/users/SashaMalysheva/followers","following_url":"https://api.github.com/users/SashaMalysheva/following{/other_user}","gists_url":"https://api.github.com/users/SashaMalysheva/gists{/gist_id}","starred_url":"https://api.github.com/users/SashaMalysheva/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SashaMalysheva/subscriptions","organizations_url":"https://api.github.com/users/SashaMalysheva/orgs","repos_url":"https://api.github.com/users/SashaMalysheva/repos","events_url":"https://api.github.com/users/SashaMalysheva/events{/privacy}","received_events_url":"https://api.github.com/users/SashaMalysheva/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/SashaMalysheva/java-course-hw","description":"","fork":false,"url":"https://api.github.com/repos/SashaMalysheva/java-course-hw","forks_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/forks","keys_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/teams","hooks_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/hooks","issue_events_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/events{/number}","events_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/events","assignees_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/assignees{/user}","branches_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/branches{/branch}","tags_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/tags","blobs_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/refs{/sha}","trees_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/statuses/{sha}","languages_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/languages","stargazers_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/stargazers","contributors_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/contributors","subscribers_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/subscribers","subscription_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/subscription","commits_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/commits{/sha}","git_commits_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/commits{/sha}","comments_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/comments{/number}","issue_comment_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/comments{/number}","contents_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/contents/{+path}","compare_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/merges","archive_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/downloads","issues_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues{/number}","pulls_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls{/number}","milestones_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/milestones{/number}","notifications_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/labels{/name}","releases_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/releases{/id}","deployments_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/deployments","created_at":"2016-02-14T05:17:09Z","updated_at":"2016-02-14T05:17:09Z","pushed_at":"2016-02-14T19:31:24Z","git_url":"git://github.com/SashaMalysheva/java-course-hw.git","ssh_url":"git@github.com:SashaMalysheva/java-course-hw.git","clone_url":"https://github.com/SashaMalysheva/java-course-hw.git","svn_url":"https://github.com/SashaMalysheva/java-course-hw","homepage":null,"size":5,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"SashaMalysheva:master","ref":"master","sha":"b0a13bf49bcf1643daba11b52a57a7f1d09c8e31","user":{"login":"SashaMalysheva","id":11505069,"avatar_url":"https://avatars.githubusercontent.com/u/11505069?v=3","gravatar_id":"","url":"https://api.github.com/users/SashaMalysheva","html_url":"https://github.com/SashaMalysheva","followers_url":"https://api.github.com/users/SashaMalysheva/followers","following_url":"https://api.github.com/users/SashaMalysheva/following{/other_user}","gists_url":"https://api.github.com/users/SashaMalysheva/gists{/gist_id}","starred_url":"https://api.github.com/users/SashaMalysheva/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SashaMalysheva/subscriptions","organizations_url":"https://api.github.com/users/SashaMalysheva/orgs","repos_url":"https://api.github.com/users/SashaMalysheva/repos","events_url":"https://api.github.com/users/SashaMalysheva/events{/privacy}","received_events_url":"https://api.github.com/users/SashaMalysheva/received_events","type":"User","site_admin":false},"repo":{"id":51680777,"name":"java-course-hw","full_name":"SashaMalysheva/java-course-hw","owner":{"login":"SashaMalysheva","id":11505069,"avatar_url":"https://avatars.githubusercontent.com/u/11505069?v=3","gravatar_id":"","url":"https://api.github.com/users/SashaMalysheva","html_url":"https://github.com/SashaMalysheva","followers_url":"https://api.github.com/users/SashaMalysheva/followers","following_url":"https://api.github.com/users/SashaMalysheva/following{/other_user}","gists_url":"https://api.github.com/users/SashaMalysheva/gists{/gist_id}","starred_url":"https://api.github.com/users/SashaMalysheva/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SashaMalysheva/subscriptions","organizations_url":"https://api.github.com/users/SashaMalysheva/orgs","repos_url":"https://api.github.com/users/SashaMalysheva/repos","events_url":"https://api.github.com/users/SashaMalysheva/events{/privacy}","received_events_url":"https://api.github.com/users/SashaMalysheva/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/SashaMalysheva/java-course-hw","description":"","fork":false,"url":"https://api.github.com/repos/SashaMalysheva/java-course-hw","forks_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/forks","keys_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/teams","hooks_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/hooks","issue_events_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/events{/number}","events_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/events","assignees_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/assignees{/user}","branches_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/branches{/branch}","tags_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/tags","blobs_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/refs{/sha}","trees_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/statuses/{sha}","languages_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/languages","stargazers_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/stargazers","contributors_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/contributors","subscribers_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/subscribers","subscription_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/subscription","commits_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/commits{/sha}","git_commits_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/git/commits{/sha}","comments_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/comments{/number}","issue_comment_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/comments{/number}","contents_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/contents/{+path}","compare_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/merges","archive_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/downloads","issues_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues{/number}","pulls_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls{/number}","milestones_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/milestones{/number}","notifications_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/labels{/name}","releases_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/releases{/id}","deployments_url":"https://api.github.com/repos/SashaMalysheva/java-course-hw/deployments","created_at":"2016-02-14T05:17:09Z","updated_at":"2016-02-14T05:17:09Z","pushed_at":"2016-02-14T19:31:24Z","git_url":"git://github.com/SashaMalysheva/java-course-hw.git","ssh_url":"git@github.com:SashaMalysheva/java-course-hw.git","clone_url":"https://github.com/SashaMalysheva/java-course-hw.git","svn_url":"https://github.com/SashaMalysheva/java-course-hw","homepage":null,"size":5,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1"},"html":{"href":"https://github.com/SashaMalysheva/java-course-hw/pull/1"},"issue":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/1"},"comments":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/SashaMalysheva/java-course-hw/statuses/721ae6b9724f0fbf1f227b5101fc43e281d1406d"}}}}