|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.applications.algorithms.machinery; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.junit.jupiter.api.Assertions.fail; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 30 | + |
| 31 | +class AlgorithmMachineryTest { |
| 32 | + @Test |
| 33 | + void shouldRunAlgorithm() { |
| 34 | + var algorithmMachinery = new AlgorithmMachinery(); |
| 35 | + |
| 36 | + var progressTracker = mock(ProgressTracker.class); |
| 37 | + var result = algorithmMachinery.runAlgorithmsAndManageProgressTracker( |
| 38 | + new RegurgitatingAlgorithm("Hello, world!"), |
| 39 | + progressTracker, |
| 40 | + false |
| 41 | + ); |
| 42 | + |
| 43 | + assertThat(result).isEqualTo("Hello, world!"); |
| 44 | + |
| 45 | + verifyNoInteractions(progressTracker); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void shouldReleaseProgressTrackerWhenAsked() { |
| 50 | + var algorithmMachinery = new AlgorithmMachinery(); |
| 51 | + |
| 52 | + var progressTracker = mock(ProgressTracker.class); |
| 53 | + var result = algorithmMachinery.runAlgorithmsAndManageProgressTracker( |
| 54 | + new RegurgitatingAlgorithm("Dodgers win world series!"), |
| 55 | + progressTracker, |
| 56 | + true |
| 57 | + ); |
| 58 | + |
| 59 | + assertThat(result).isEqualTo("Dodgers win world series!"); |
| 60 | + |
| 61 | + verify(progressTracker).release(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldMarkProgressTracker() { |
| 66 | + var algorithmMachinery = new AlgorithmMachinery(); |
| 67 | + |
| 68 | + var progressTracker = mock(ProgressTracker.class); |
| 69 | + var exception = new RuntimeException("Whoops!"); |
| 70 | + try { |
| 71 | + algorithmMachinery.runAlgorithmsAndManageProgressTracker( |
| 72 | + new FailingAlgorithm(exception), |
| 73 | + progressTracker, |
| 74 | + false |
| 75 | + ); |
| 76 | + fail(); |
| 77 | + } catch (Exception e) { |
| 78 | + assertThat(e).hasMessage("Whoops!"); |
| 79 | + } |
| 80 | + |
| 81 | + verify(progressTracker).endSubTaskWithFailure(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void shouldMarkProgressTrackerAndReleaseIt() { |
| 86 | + var algorithmMachinery = new AlgorithmMachinery(); |
| 87 | + |
| 88 | + var progressTracker = mock(ProgressTracker.class); |
| 89 | + var exception = new RuntimeException("Yeah, no..."); |
| 90 | + try { |
| 91 | + algorithmMachinery.runAlgorithmsAndManageProgressTracker( |
| 92 | + new FailingAlgorithm(exception), |
| 93 | + progressTracker, |
| 94 | + true |
| 95 | + ); |
| 96 | + fail(); |
| 97 | + } catch (Exception e) { |
| 98 | + assertThat(e).hasMessage("Yeah, no..."); |
| 99 | + } |
| 100 | + |
| 101 | + verify(progressTracker).endSubTaskWithFailure(); |
| 102 | + verify(progressTracker).release(); |
| 103 | + } |
| 104 | +} |
0 commit comments