-
Notifications
You must be signed in to change notification settings - Fork 14
Java05. Дз 06, Явейн Анна #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bronti
wants to merge
1
commit into
java-course-au:master
Choose a base branch
from
bronti:06-junit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Empty file.
35 changes: 35 additions & 0 deletions
35
src/main/java/ru/spbau/yaveyn/java/junithw/MemoryLeakLimit.java
This file contains hidden or 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,35 @@ | ||
| package ru.spbau.yaveyn.java.junithw; | ||
|
|
||
| /** | ||
| * Created by bronti on 04.05.17. | ||
| */ | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.rules.TestRule; | ||
| import org.junit.runner.Description; | ||
| import org.junit.runners.model.Statement; | ||
|
|
||
| public class MemoryLeakLimit implements TestRule { | ||
| private long mbLimit; | ||
|
|
||
| public Statement apply(final Statement base, Description description) { | ||
| return new Statement() { | ||
| @Override | ||
| public void evaluate() throws Throwable { | ||
| Runtime rt = Runtime.getRuntime(); | ||
| System.gc(); | ||
| long beforeUsedMb = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024; | ||
| base.evaluate(); | ||
| long afterUsedMb = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024; | ||
| System.gc(); | ||
| Assert.assertTrue( | ||
| String.format("Memory leak detected (%1$d Mb > %2$d Mb)", afterUsedMb - beforeUsedMb, mbLimit), | ||
| afterUsedMb - beforeUsedMb <= mbLimit); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public void limit(long mb) { | ||
| this.mbLimit = mb; | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
src/main/java/ru/spbau/yaveyn/java/junithw/ThreadExpectedException.java
This file contains hidden or 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,64 @@ | ||
| package ru.spbau.yaveyn.java.junithw; | ||
|
|
||
| import com.sun.org.apache.xpath.internal.operations.Bool; | ||
| import org.junit.Assert; | ||
| import org.junit.rules.TestRule; | ||
| import org.junit.runner.Description; | ||
| import org.junit.runners.model.Statement; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Queue; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
|
||
| /** | ||
| * Created by bronti on 04.05.17. | ||
| */ | ||
| public class ThreadExpectedException implements TestRule { | ||
|
|
||
| private Class<? extends Throwable> expectedThrowable; | ||
| private Queue<Thread> registeredThreads = new ConcurrentLinkedQueue<>(); | ||
| private final Queue<Boolean> threadCorectness = new ConcurrentLinkedQueue<>(); | ||
|
|
||
| public void expect(Class<? extends Throwable> e){ | ||
| expectedThrowable = e; | ||
| } | ||
|
|
||
| public void registerThread(Thread t) { | ||
| registeredThreads.add(t); | ||
| final Class<? extends Throwable> currentThrowable = expectedThrowable; | ||
| t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { | ||
| @Override | ||
| public void uncaughtException(Thread t, Throwable e) { | ||
| if (currentThrowable.equals(e.getClass())) { | ||
| threadCorectness.add(true); | ||
| } | ||
| } | ||
| }); | ||
| // t.start(); | ||
| // не уверена, что правильно поняла задание, если честно.. | ||
| } | ||
|
|
||
| @Override | ||
| public Statement apply(final Statement base, Description description) { | ||
| return new Statement() { | ||
| @Override | ||
| public void evaluate() throws Throwable { | ||
| base.evaluate(); | ||
| String msg = "Some thread is running or did not throw expected exception."; | ||
| if (expectedThrowable != null) { | ||
| Assert.assertEquals(msg, registeredThreads.size(), threadCorectness.size()); | ||
| } | ||
| for (Thread t : registeredThreads) { | ||
| t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { | ||
| @Override | ||
| public void uncaughtException(Thread t, Throwable e) {} | ||
| }); | ||
| } | ||
| registeredThreads.clear(); | ||
| threadCorectness.clear(); | ||
| // судя по заданию, если expectedThrowable -- null, то не надо ничего проверять. Надеюсь, я правильно поняла. | ||
| } | ||
| }; | ||
| } | ||
| } |
Empty file.
27 changes: 27 additions & 0 deletions
27
src/test/java/ru/spbau/yaveyn/java/junithw/MemoryLeakLimitTest.java
This file contains hidden or 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,27 @@ | ||
| package ru.spbau.yaveyn.java.junithw; | ||
|
|
||
| import org.junit.Ignore; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Created by bronti on 04.05.17. | ||
| */ | ||
| public class MemoryLeakLimitTest { | ||
|
|
||
| @Rule | ||
| public MemoryLeakLimit mll = new MemoryLeakLimit(); | ||
|
|
||
|
|
||
|
|
||
| @Test | ||
| public void testSuccess() { | ||
| mll.limit(1); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore | ||
| public void testFail() { | ||
| mll.limit(-1); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
src/test/java/ru/spbau/yaveyn/java/junithw/ThreadExpectedExceptionTest.java
This file contains hidden or 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,75 @@ | ||
| package ru.spbau.yaveyn.java.junithw; | ||
|
|
||
| import com.sun.javaws.exceptions.InvalidArgumentException; | ||
| import org.junit.Ignore; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import sun.plugin.dom.exception.InvalidStateException; | ||
|
|
||
| /** | ||
| * Created by bronti on 04.05.17. | ||
| */ | ||
| public class ThreadExpectedExceptionTest { | ||
|
|
||
| @Rule | ||
| public ThreadExpectedException tee = new ThreadExpectedException(); | ||
|
|
||
|
|
||
|
|
||
| @Test | ||
| public void testSuccess() { | ||
| Thread t = new Thread () { | ||
| @Override | ||
| public void run(){ | ||
| throw new InvalidStateException(""); | ||
| } | ||
| }; | ||
| tee.expect(InvalidStateException.class); | ||
| tee.registerThread(t); | ||
| t.start(); | ||
| try { | ||
| Thread.sleep(100); | ||
| } | ||
| catch (Exception e) { | ||
| // do nothing | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotExpected() { | ||
| Thread t = new Thread () { | ||
| @Override | ||
| public void run(){ | ||
| throw new InvalidStateException(""); | ||
| } | ||
| }; | ||
| tee.registerThread(t); | ||
| t.start(); | ||
| try { | ||
| Thread.sleep(100); | ||
| } | ||
| catch (Exception e) { | ||
| // do nothing | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore | ||
| public void testFail() { | ||
| Thread t = new Thread () { | ||
| @Override | ||
| public void run(){ | ||
| throw new InvalidStateException(""); | ||
| } | ||
| }; | ||
| tee.expect(InvalidArgumentException.class); | ||
| tee.registerThread(t); | ||
| t.start(); | ||
| try { | ||
| Thread.sleep(100); | ||
| } | ||
| catch (Exception e) { | ||
| // do nothing | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
думаю это должно стоять на 1 строку выше)