From commits-return-1752-archive-asf-public=cust-asf.ponee.io@fineract.apache.org Mon Jan 22 16:53:08 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id CB092180799 for ; Mon, 22 Jan 2018 16:53:07 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id BA4DF160C3A; Mon, 22 Jan 2018 15:53:07 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DD6DA160C4D for ; Mon, 22 Jan 2018 16:53:06 +0100 (CET) Received: (qmail 10175 invoked by uid 500); 22 Jan 2018 15:53:06 -0000 Mailing-List: contact commits-help@fineract.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@fineract.apache.org Delivered-To: mailing list commits@fineract.apache.org Received: (qmail 10061 invoked by uid 99); 22 Jan 2018 15:53:05 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Jan 2018 15:53:05 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id A266682094; Mon, 22 Jan 2018 15:53:04 +0000 (UTC) Date: Mon, 22 Jan 2018 15:53:11 +0000 To: "commits@fineract.apache.org" Subject: [fineract-cn-lang] 07/08: Moved EventExpectation and related code from provisioner to lang. Wrote unit test. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: myrle@apache.org In-Reply-To: <151663638449.12103.7813901531240893754@gitbox.apache.org> References: <151663638449.12103.7813901531240893754@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: fineract-cn-lang X-Git-Refname: refs/heads/develop X-Git-Reftype: branch X-Git-Rev: 116e59f3be3ab4f6c720fcce6289e2623870fa62 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20180122155304.A266682094@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. myrle pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/fineract-cn-lang.git commit 116e59f3be3ab4f6c720fcce6289e2623870fa62 Author: Myrle Krantz AuthorDate: Thu Sep 21 09:44:15 2017 +0200 Moved EventExpectation and related code from provisioner to lang. Wrote unit test. --- build.gradle | 3 +- .../core/lang/listening/EventExpectation.java | 88 ++++++++++++++++++++++ .../io/mifos/core/lang/listening/EventKey.java | 61 +++++++++++++++ .../core/lang/listening/TenantedEventListener.java | 47 ++++++++++++ .../lang/listening/TenantedEventListenerTest.java | 81 ++++++++++++++++++++ 5 files changed, 279 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9741575..e6eafe8 100644 --- a/build.gradle +++ b/build.gradle @@ -104,5 +104,6 @@ license { java = 'SLASHSTAR_STYLE' } ext.year = Calendar.getInstance().get(Calendar.YEAR) - ext.name = 'The Mifos Initiative' + ext.name = 'Kuelap, Inc' + skipExistingHeaders true } \ No newline at end of file diff --git a/src/main/java/io/mifos/core/lang/listening/EventExpectation.java b/src/main/java/io/mifos/core/lang/listening/EventExpectation.java new file mode 100644 index 0000000..1e6f248 --- /dev/null +++ b/src/main/java/io/mifos/core/lang/listening/EventExpectation.java @@ -0,0 +1,88 @@ +/* + * Copyright 2017 Kuelap, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.mifos.core.lang.listening; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @author Myrle Krantz + */ +public class EventExpectation { + private final EventKey key; + private boolean eventFound = false; + private boolean eventWithdrawn = false; + + private final ReentrantLock lock = new ReentrantLock(); + + private final Condition found = lock.newCondition(); + + EventExpectation(final EventKey key) { + this.key = key; + } + + EventKey getKey() { + return key; + } + + void setEventFound() { + lock.lock(); + try { + this.eventFound = true; + found.signal(); + } + finally { + lock.unlock(); + } + } + + void setEventWithdrawn() { + lock.lock(); + try { + this.eventWithdrawn = true; + found.signal(); + } + finally { + lock.unlock(); + } + } + + @SuppressWarnings("WeakerAccess") + public boolean waitForOccurrence(long timeout, TimeUnit timeUnit) throws InterruptedException { + + lock.lock(); + try { + if (eventFound) + return true; + + if (eventWithdrawn) + return false; + + found.await(timeout, timeUnit); + + return (eventFound); + } + finally { + lock.unlock(); + } + } + + @Override + public String toString() { + return key.toString(); + } +} diff --git a/src/main/java/io/mifos/core/lang/listening/EventKey.java b/src/main/java/io/mifos/core/lang/listening/EventKey.java new file mode 100644 index 0000000..9026288 --- /dev/null +++ b/src/main/java/io/mifos/core/lang/listening/EventKey.java @@ -0,0 +1,61 @@ +/* + * Copyright 2017 Kuelap, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.mifos.core.lang.listening; + +import java.util.Objects; + +/** + * @author Myrle Krantz + */ +@SuppressWarnings("WeakerAccess") +public class EventKey { + private String tenantIdentifier; + private String eventName; + private Object event; + + public EventKey( + final String tenantIdentifier, + final String eventName, + final Object event) { + this.tenantIdentifier = tenantIdentifier; + this.eventName = eventName; + this.event = event; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + EventKey eventKey = (EventKey) o; + return Objects.equals(tenantIdentifier, eventKey.tenantIdentifier) && + Objects.equals(eventName, eventKey.eventName) && + Objects.equals(event, eventKey.event); + } + + @Override + public int hashCode() { + return Objects.hash(tenantIdentifier, eventName, event); + } + + @Override + public String toString() { + return "EventKey{" + + "tenantIdentifier='" + tenantIdentifier + '\'' + + ", eventName='" + eventName + '\'' + + ", event=" + event + + '}'; + } +} diff --git a/src/main/java/io/mifos/core/lang/listening/TenantedEventListener.java b/src/main/java/io/mifos/core/lang/listening/TenantedEventListener.java new file mode 100644 index 0000000..9993097 --- /dev/null +++ b/src/main/java/io/mifos/core/lang/listening/TenantedEventListener.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017 Kuelap, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.mifos.core.lang.listening; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author Myrle Krantz + */ +@SuppressWarnings("WeakerAccess") +public class TenantedEventListener { + private final Map eventExpectations = new ConcurrentHashMap<>(); + + public EventExpectation expect(final EventKey eventKey) { + final EventExpectation value = new EventExpectation(eventKey); + eventExpectations.put(eventKey, value); + return value; + } + + public void notify(final EventKey eventKey) { + final EventExpectation eventExpectation = eventExpectations.remove(eventKey); + if (eventExpectation != null) { + eventExpectation.setEventFound(); + } + } + + public void withdrawExpectation(final EventExpectation eventExpectation) { + final EventExpectation expectation = eventExpectations.remove(eventExpectation.getKey()); + if (expectation != null) { + eventExpectation.setEventWithdrawn(); + } + } +} \ No newline at end of file diff --git a/src/test/java/io/mifos/core/lang/listening/TenantedEventListenerTest.java b/src/test/java/io/mifos/core/lang/listening/TenantedEventListenerTest.java new file mode 100644 index 0000000..ed919c0 --- /dev/null +++ b/src/test/java/io/mifos/core/lang/listening/TenantedEventListenerTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2017 Kuelap, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.mifos.core.lang.listening; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +/** + * @author Myrle Krantz + */ +public class TenantedEventListenerTest { + static class SomeState { + boolean mutableBit = false; + } + + @Test + public void shouldWaitForEventOccurenceTrue() throws InterruptedException { + final TenantedEventListener testSubject = new TenantedEventListener(); + final EventKey eventKey = new EventKey("shouldWaitForEventOccurenceTrue", "blah", "blah"); + final EventExpectation eventExpectation = testSubject.expect(eventKey); + final SomeState someState = stateThatMutatesIn60MillisThenNotifiesOnEventKey(testSubject, eventKey); + + Assert.assertTrue(eventExpectation.waitForOccurrence(600, TimeUnit.MILLISECONDS)); + Assert.assertTrue(someState.mutableBit); + } + + @Test + public void shouldWaitForEventOccurenceFalse() throws InterruptedException { + final TenantedEventListener testSubject = new TenantedEventListener(); + final EventKey eventKey = new EventKey("shouldWaitForEventOccurenceFalse", "blah", "blah"); + final EventExpectation eventExpectation = testSubject.expect(eventKey); + final SomeState someState = stateThatMutatesIn60MillisThenNotifiesOnEventKey(testSubject, eventKey); + + Assert.assertFalse(eventExpectation.waitForOccurrence(6, TimeUnit.MILLISECONDS)); + Assert.assertFalse(someState.mutableBit); //Some potential for flakiness here, because this is timing dependent. + } + + @Test + public void shouldNotWaitForEventOccurence() throws InterruptedException { + final TenantedEventListener testSubject = new TenantedEventListener(); + final EventKey eventKey = new EventKey("shouldNotWaitForEventOccurence", "blah", "blah"); + final EventExpectation eventExpectation = testSubject.expect(eventKey); + final SomeState someState = stateThatMutatesIn60MillisThenNotifiesOnEventKey(testSubject, eventKey); + + testSubject.withdrawExpectation(eventExpectation); + + Assert.assertFalse(eventExpectation.waitForOccurrence(600, TimeUnit.MILLISECONDS)); + Assert.assertFalse(someState.mutableBit); //Some potential for flakiness here, because this is timing dependent. + } + + private SomeState stateThatMutatesIn60MillisThenNotifiesOnEventKey( + final TenantedEventListener testSubject, + final EventKey eventKey) { + final SomeState someState = new SomeState(); + new Thread(() -> { + try { + TimeUnit.MILLISECONDS.sleep(60); + someState.mutableBit = true; + testSubject.notify(eventKey); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + return someState; + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact myrle@apache.org.