This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdc1328
commit cf8ec9a
Showing
16 changed files
with
243 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
errors: | ||
- amihaiemil@gmail.com |
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/main/java/com/amihaiemil/charles/github/AuthorizedRequest.java
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/main/java/com/amihaiemil/charles/github/RtNotifications.java
This file contains 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
102 changes: 102 additions & 0 deletions
102
src/main/java/com/amihaiemil/charles/github/SmartNotifications.java
This file contains 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,102 @@ | ||
/** | ||
* Copyright (c) 2016-2017, 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: | ||
* 1)Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2)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. | ||
* 3)Neither the name of mention-notifications-ejb 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.github; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.json.JsonObject; | ||
|
||
/** | ||
* Smart notifications. If there are no new notifications for a few | ||
* checks in a row, it will skip a few next checks, thus saving bandwidth | ||
* and making less HTTP calls. This is all based on the assumption that | ||
* notifications aren't so frequent, so it makes no sense to always, blindly | ||
* repeat the cycle. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 1.2.0 | ||
* @todo #13:30m/DEV Let's make this configurable. Currently it is | ||
* hard-coded to 3 empty Checks. It should configurable via the system | ||
* property skip.empty.notifications. | ||
*/ | ||
public final class SmartNotifications implements Notifications { | ||
|
||
/** | ||
* Original notifications to use. | ||
*/ | ||
private Notifications original; | ||
|
||
/** | ||
* Count of empty notifications that can pass. | ||
*/ | ||
private int allowedEmpty = 3; | ||
|
||
/** | ||
* Nr of consecutive empty checks. | ||
*/ | ||
private int empty = 0; | ||
|
||
/** | ||
* Skip this fetch, or not? | ||
*/ | ||
private boolean skip = false; | ||
|
||
/** | ||
* Ctor. | ||
* @param orig Given Notifications | ||
*/ | ||
public SmartNotifications(final Notifications orig) { | ||
this.original = orig; | ||
} | ||
|
||
@Override | ||
public List<JsonObject> fetch() throws IOException { | ||
List<JsonObject> notifications = new ArrayList<>(); | ||
if(!this.skip) { | ||
if(this.allowedEmpty == 0) { | ||
this.allowedEmpty = 3; | ||
this.empty = 0; | ||
} | ||
notifications = this.original.fetch(); | ||
if(notifications.isEmpty()) { | ||
this.empty++; | ||
} else { | ||
this.empty = 0; | ||
} | ||
} else { | ||
this.allowedEmpty--; | ||
} | ||
this.skip = this.empty >= this.allowedEmpty && this.allowedEmpty > 0; | ||
return notifications; | ||
} | ||
|
||
@Override | ||
public void markAsRead() throws IOException { | ||
this.original.markAsRead(); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/test/java/com/amihaiemil/charles/github/AuthorizationTestCase.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/test/java/com/amihaiemil/charles/github/MentionTestCase.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/test/java/com/amihaiemil/charles/github/NtPostTestCase.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/test/java/com/amihaiemil/charles/github/RtNotificationsTestCase.java
This file contains 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
117 changes: 117 additions & 0 deletions
117
src/test/java/com/amihaiemil/charles/github/SmartNotificationsTestcase.java
This file contains 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,117 @@ | ||
/** | ||
* Copyright (c) 2016-2017, 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: | ||
* 1)Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2)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. | ||
* 3)Neither the name of mention-notifications-ejb 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.github; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
|
||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
/** | ||
* Unit tests for {@link SmartNotifications} | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $id$ | ||
* @since 1.2.0 | ||
*/ | ||
public final class SmartNotificationsTestcase { | ||
|
||
/** | ||
* SmartNotifications can skip 3 checks if the the previous | ||
* 3 notifications came out empty. | ||
* @throws IOException If something goes wrong. | ||
*/ | ||
@Test | ||
public void skipsThreeChecksAfterThreeEmptyChecks() throws IOException { | ||
List<JsonObject> found = new ArrayList<>(); | ||
found.add( | ||
Json.createObjectBuilder().add("found", "testnotification").build() | ||
); | ||
Notifications three = Mockito.mock(Notifications.class); | ||
Mockito.when(three.fetch()) | ||
.thenReturn(new ArrayList<JsonObject>()) | ||
.thenReturn(new ArrayList<JsonObject>()) | ||
.thenReturn(new ArrayList<JsonObject>()) | ||
.thenReturn(found); | ||
Notifications smart = new SmartNotifications(three); | ||
for(int i=0;i<6;i++) { | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.is(Matchers.empty()) | ||
); | ||
} | ||
List<JsonObject> fetched = smart.fetch(); | ||
MatcherAssert.assertThat(fetched.size(), Matchers.is(1)); | ||
MatcherAssert.assertThat( | ||
fetched.get(0).getString("found"), | ||
Matchers.equalTo("testnotification") | ||
); | ||
Mockito.verify(three, Mockito.times(4)).fetch(); | ||
} | ||
|
||
/** | ||
* SmartNotifications continues fetching notifications if less than 3 | ||
* empty checks happened in a row. | ||
* @throws IOException If something goes wrong. | ||
*/ | ||
@Test | ||
public void doesntSkipAfterTwoEmptyChecks() throws IOException { | ||
List<JsonObject> found = new ArrayList<>(); | ||
found.add( | ||
Json.createObjectBuilder().add("found", "testnotification").build() | ||
); | ||
Notifications three = Mockito.mock(Notifications.class); | ||
Mockito.when(three.fetch()) | ||
.thenReturn(new ArrayList<JsonObject>()) | ||
.thenReturn(new ArrayList<JsonObject>()) | ||
.thenReturn(found); | ||
Notifications smart = new SmartNotifications(three); | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.is(Matchers.empty()) | ||
); | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.is(Matchers.empty()) | ||
); | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty())) | ||
); | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty())) | ||
); | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty())) | ||
); | ||
MatcherAssert.assertThat( | ||
smart.fetch(), Matchers.not(Matchers.is(Matchers.empty())) | ||
); | ||
Mockito.verify(three, Mockito.times(6)).fetch(); | ||
} | ||
} |