Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #64 from amihaiemil/60
Browse files Browse the repository at this point in the history
SwitchableCrawl + unit tests
  • Loading branch information
amihaiemil committed Nov 4, 2016
2 parents f6a2a53 + 80baab1 commit 9c446fa
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/main/java/com/amihaiemil/charles/SwitchableCrawl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright (c) 2016, Mihai Emil Andronache
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of charles nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.charles;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A crawl that switches to a new one if it fails with RuntimeException.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*
*/
public final class SwitchableCrawl implements WebCrawl {

private static final Logger LOG = LoggerFactory.getLogger(SwitchableCrawl.class);

/**
* Initial crawl.
*/
private WebCrawl initial;

/**
* Failsafe. Run in case the initial crawl fails.
*/
private WebCrawl failsafe;

/**
* Ctor.
* @param initial WebCrawl performed.
* @param failsafe WebCrawl performed in case the initial one fails with RuntimeException.
*/
public SwitchableCrawl(WebCrawl initial, WebCrawl failsafe) {
this.initial = initial;
this.failsafe = failsafe;
}

@Override
public void crawl() throws DataExportException {
try {
this.initial.crawl();
} catch (RuntimeException ex) {
LOG.error("The initial crawl failed. Running the failsafe crawl...", ex);
this.failsafe.crawl();
}
}

}
22 changes: 22 additions & 0 deletions src/test/java/com/amihaiemil/charles/RetriableCrawlTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ public void crawlSuccessfulAtLastTrial() throws Exception {
rc.crawl();
Mockito.verify(mc, Mockito.times(5)).crawl();
}

/**
* RetriableCrawl fails with RE the first time, then throws DataExportException
* the second time (thus not retrying the 3rd time)
* @throws Exception If something goes wrong.
*/
@Test
public void crawlThrowsDeeSecondTime() throws Exception {
WebCrawl mc = Mockito.mock(WebCrawl.class);
Mockito
.doThrow(new RuntimeException("Test runtime exception 1"))
.doThrow(new DataExportException("Test dee exception 2"))
.doNothing()
.when(mc).crawl();
RetriableCrawl rc = new RetriableCrawl(mc);
try {
rc.crawl();
} catch (DataExportException ex) {
assertTrue(ex.getMessage().equals("Test dee exception 2"));
Mockito.verify(mc, Mockito.times(2)).crawl();
}
}

/**
* RetriableCrawl fails after all retrials
Expand Down
120 changes: 120 additions & 0 deletions src/test/java/com/amihaiemil/charles/SwitchableCrawlTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright (c) 2016, Mihai Emil Andronache
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of charles nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.charles;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.mockito.Mockito;

/**
* Unit tests for {@link SwitchableCrawl}
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*
*/
public final class SwitchableCrawlTestCase {

/**
* {@link SwitchableCrawl} performs the initial crawl without problems.
* @throws Exception If something goes wrong.
*/
@Test
public void initialCrawlWorksFine() throws Exception {
WebCrawl initial = Mockito.mock(WebCrawl.class);
WebCrawl failsafe = Mockito.mock(WebCrawl.class);
Mockito.doNothing().when(initial).crawl();
SwitchableCrawl sc = new SwitchableCrawl(initial, failsafe);
sc.crawl();
Mockito.verify(initial, Mockito.times(1)).crawl();
Mockito.verify(failsafe, Mockito.times(0)).crawl();
}

/**
* {@link SwitchableCrawl} performs the initial crawl, which throws DataExportException
* @throws Exception If something goes wrong.
*/
@Test
public void initialCrawlThrowsDee() throws Exception {
WebCrawl initial = Mockito.mock(WebCrawl.class);
WebCrawl failsafe = Mockito.mock(WebCrawl.class);
Mockito
.doThrow(new DataExportException("Dee on initial crawl!"))
.when(initial).crawl();
SwitchableCrawl sc = new SwitchableCrawl(initial, failsafe);
try {
sc.crawl();
} catch (DataExportException ex) {
assertTrue(ex.getMessage().equals("Dee on initial crawl!"));
Mockito.verify(initial, Mockito.times(1)).crawl();
Mockito.verify(failsafe, Mockito.times(0)).crawl();
}
}

/**
* {@link SwitchableCrawl} performs the failsafe crawl without problems, after
* the initial one failed
* @throws Exception If something goes wrong.
*/
@Test
public void failsafeWorksFine() throws Exception {
WebCrawl initial = Mockito.mock(WebCrawl.class);
WebCrawl failsafe = Mockito.mock(WebCrawl.class);
Mockito
.doThrow(new RuntimeException("Fail initial crawl!"))
.when(initial).crawl();
Mockito.doNothing().when(failsafe).crawl();
SwitchableCrawl sc = new SwitchableCrawl(initial, failsafe);
sc.crawl();
Mockito.verify(initial, Mockito.times(1)).crawl();
Mockito.verify(failsafe, Mockito.times(1)).crawl();
}

/**
* {@link SwitchableCrawl} throws RE after both crawls fail.
* @throws Exception If something goes wrong.
*/
@Test
public void bothCrawlsFail() throws Exception {
WebCrawl initial = Mockito.mock(WebCrawl.class);
WebCrawl failsafe = Mockito.mock(WebCrawl.class);
Mockito
.doThrow(new RuntimeException("Fail initial crawl!"))
.when(initial).crawl();
Mockito
.doThrow(new RuntimeException("Fail failsafe crawl!"))
.when(failsafe).crawl();
SwitchableCrawl sc = new SwitchableCrawl(initial, failsafe);
try {
sc.crawl();
} catch (RuntimeException ex) {
assertTrue(ex.getMessage().equals("Fail failsafe crawl!"));
Mockito.verify(initial, Mockito.times(1)).crawl();
Mockito.verify(failsafe, Mockito.times(1)).crawl();
}
}
}

0 comments on commit 9c446fa

Please sign in to comment.