Skip to content

Commit

Permalink
Merge pull request #1810 from dhis2/androsdk-1562
Browse files Browse the repository at this point in the history
fix: [ANDROSDK-1562] Make period generation methods synchronized
  • Loading branch information
vgarciabnz authored Jun 23, 2022
2 parents cb5d35d + c6a4802 commit cc17401
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Locale;

@SuppressWarnings({"PMD.AvoidSynchronizedAtMethodLevel"})
abstract class AbstractPeriodGenerator implements PeriodGenerator {
private final Calendar initialCalendar;
protected Calendar calendar;
Expand All @@ -54,7 +55,7 @@ abstract class AbstractPeriodGenerator implements PeriodGenerator {
}

@Override
public final List<Period> generatePeriods(int start, int end) throws RuntimeException {
synchronized public final List<Period> generatePeriods(int start, int end) throws RuntimeException {
this.calendar = (Calendar) initialCalendar.clone();

if (start >= end) {
Expand All @@ -77,7 +78,7 @@ public final List<Period> generatePeriods(int start, int end) throws RuntimeExce
}

@Override
public final Period generatePeriod(Date date, int periodOffset) {
synchronized public final Period generatePeriod(Date date, int periodOffset) {
this.calendar = (Calendar) initialCalendar.clone();

moveToStartOfThePeriodOfADayWithOffset(date, periodOffset);
Expand All @@ -99,7 +100,7 @@ public final Period generatePeriod(Date date, int periodOffset) {
}

@Override
public List<Period> generatePeriodsInYear(int yearOffset) {
synchronized public List<Period> generatePeriodsInYear(int yearOffset) {
this.calendar = (Calendar) initialCalendar.clone();

int targetYear = calendar.get(Calendar.YEAR) + yearOffset;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* 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 the HISP project 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 OWNER 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 org.hisp.dhis.android.core.period.internal

import com.google.common.truth.Truth.assertThat
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import java.util.*
import org.hisp.dhis.android.core.arch.helpers.DateUtils
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class PeriodGeneratorShould {

private val calendar = Calendar.getInstance()

private val dailyGenerator: PeriodGenerator = DailyPeriodGenerator(calendar)
private val weeklyGenerator: PeriodGenerator = WeeklyPeriodGeneratorFactory.weekly(calendar)
private val monthlyGenerator: PeriodGenerator = MonthlyPeriodGenerator(calendar)

@Test
fun generate_single_period_simultaneously() {
val refDate = DateUtils.SIMPLE_DATE_FORMAT.parse("2019-12-30")

val periods = (-500..500).map {
Single.fromCallable {
dailyGenerator.generatePeriod(refDate, it)
weeklyGenerator.generatePeriod(refDate, it)
monthlyGenerator.generatePeriod(refDate, it)
}.subscribeOn(Schedulers.io())
}

Single.merge(periods).blockingSubscribe()

assertThat(periods.size).isEqualTo(1001)
}

@Test
fun generate_multiple_periods_simultaneously() {
val periods = (0..100).map {
Single.fromCallable {
dailyGenerator.generatePeriods(it - 100, it)
weeklyGenerator.generatePeriods(it - 100, it)
monthlyGenerator.generatePeriods(it - 100, it)
}.subscribeOn(Schedulers.io())
}

Single.merge(periods).blockingSubscribe()

assertThat(periods.size).isEqualTo(101)
}

@Test
fun generate_periods_in_year_simultaneously() {
val periods = (-5..5).map {
Single.fromCallable {
dailyGenerator.generatePeriodsInYear(it)
weeklyGenerator.generatePeriodsInYear(it)
monthlyGenerator.generatePeriodsInYear(it)
}.subscribeOn(Schedulers.io())
}

Single.merge(periods).blockingSubscribe()

assertThat(periods.size).isEqualTo(11)
}
}

0 comments on commit cc17401

Please sign in to comment.