-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
bf33239
commit 1d0248b
Showing
4 changed files
with
157 additions
and
4 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
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
138 changes: 138 additions & 0 deletions
138
...s/src/test/java/io/eventdriven/uniqueness/users/UserEmailRegistrationScavengingTests.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,138 @@ | ||
package io.eventdriven.uniqueness.users; | ||
|
||
import com.eventstore.dbclient.EventStoreDBClient; | ||
import com.eventstore.dbclient.EventStoreDBClientSettings; | ||
import com.eventstore.dbclient.EventStoreDBConnectionString; | ||
import com.eventstore.dbclient.ParseError; | ||
import io.eventdriven.uniqueness.core.esdb.EventStore; | ||
import io.eventdriven.uniqueness.core.resourcereservation.Hash; | ||
import io.eventdriven.uniqueness.core.resourcereservation.esdb.ESDBResourceReservationHandler; | ||
import io.eventdriven.uniqueness.core.resourcereservation.jpa.ResourceReservation; | ||
import io.eventdriven.uniqueness.core.resourcereservation.jpa.ResourceReservationRepository; | ||
import io.eventdriven.uniqueness.core.resourcereservation.jpa.ResourceReservationScavenging; | ||
import io.eventdriven.uniqueness.core.retries.NulloRetryPolicy; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.time.Duration; | ||
import java.time.OffsetDateTime; | ||
import java.util.UUID; | ||
|
||
|
||
import static io.eventdriven.uniqueness.core.resourcereservation.esdb.ResourceReservationEvent.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@DataJpaTest | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
@RunWith(SpringRunner.class) | ||
public class UserEmailRegistrationScavengingTests { | ||
|
||
@Test | ||
public void scavenging_cleansReservationAndLookup() { | ||
// Given | ||
for (var reservation : toScavenge) { | ||
var resourceKey = reservation.getResourceKey(); | ||
|
||
repository.save(reservation); | ||
eventStore.append(getReservationStreamId(resourceKey), | ||
new ResourceReservationInitiated(resourceKey, reservation.getInitiatedAt(), Duration.ofMinutes(1)) | ||
); | ||
} | ||
|
||
for (var reservation : toKeep) { | ||
var resourceKey = reservation.getResourceKey(); | ||
|
||
repository.save(reservation); | ||
|
||
eventStore.append(getReservationStreamId(resourceKey), | ||
new ResourceReservationInitiated(resourceKey, reservation.getInitiatedAt(), Duration.ofMinutes(1)), | ||
new ResourceReservationConfirmed(resourceKey, reservation.getReservedAt()) | ||
); | ||
} | ||
|
||
// When | ||
resourceReservationScavenging.scavengeTimedOut(OffsetDateTime.now()); | ||
|
||
// Then | ||
for (var reservation : toScavenge) { | ||
var resourceKey = reservation.getResourceKey(); | ||
|
||
assertFalse(repository.existsById(resourceKey)); | ||
assertInstanceOf(EventStore.ReadResult.StreamDoesNotExist.class, eventStore.read(getReservationStreamId(resourceKey))); | ||
} | ||
|
||
for (var reservation : toKeep) { | ||
var resourceKey = reservation.getResourceKey(); | ||
|
||
assertTrue(repository.existsById(resourceKey)); | ||
var result = eventStore.read(getReservationStreamId(resourceKey)); | ||
assertInstanceOf(EventStore.ReadResult.Success.class, result); | ||
assertEquals(2, ((EventStore.ReadResult.Success)result).events().length); | ||
} | ||
} | ||
@Autowired | ||
private ResourceReservationRepository repository; | ||
private EventStore eventStore; | ||
|
||
private ResourceReservationScavenging resourceReservationScavenging; | ||
|
||
private String reservationStreamId; | ||
|
||
private final ResourceReservation[] toScavenge = new ResourceReservation[]{ | ||
new ResourceReservation( | ||
getRandomResourceKey(), | ||
OffsetDateTime.now().minus(Duration.ofDays(1)), | ||
ResourceReservation.Status.Pending, | ||
OffsetDateTime.now().minus(Duration.ofDays(1)).minus(Duration.ofMinutes(10)), | ||
null | ||
), | ||
new ResourceReservation( | ||
getRandomResourceKey(), | ||
OffsetDateTime.now().minus(Duration.ofMinutes(1)), | ||
ResourceReservation.Status.Pending, | ||
OffsetDateTime.now().minus(Duration.ofMinutes(1)).minus(Duration.ofSeconds(10)), | ||
OffsetDateTime.now() | ||
), | ||
}; | ||
|
||
private final ResourceReservation[] toKeep = new ResourceReservation[]{ | ||
new ResourceReservation( | ||
getRandomResourceKey(), | ||
OffsetDateTime.now().minus(Duration.ofDays(2)), | ||
ResourceReservation.Status.Confirmed, | ||
OffsetDateTime.now().minus(Duration.ofDays(22)).minus(Duration.ofMinutes(10)), | ||
null | ||
), | ||
new ResourceReservation( | ||
getRandomResourceKey(), | ||
OffsetDateTime.now().minus(Duration.ofMinutes(2)), | ||
ResourceReservation.Status.Confirmed, | ||
OffsetDateTime.now().minus(Duration.ofMinutes(2)).minus(Duration.ofSeconds(10)), | ||
OffsetDateTime.now() | ||
), | ||
}; | ||
|
||
@BeforeEach | ||
void beforeEach() throws ParseError { | ||
EventStoreDBClientSettings settings = EventStoreDBConnectionString.parse("esdb://localhost:2113?tls=false"); | ||
EventStoreDBClient eventStoreDBClient = EventStoreDBClient.create(settings); | ||
eventStore = new EventStore(eventStoreDBClient); | ||
var resourceReservationHandler = new ESDBResourceReservationHandler(Duration.ofMinutes(10), new NulloRetryPolicy(), eventStore); | ||
|
||
resourceReservationScavenging = new ResourceReservationScavenging(repository, resourceReservationHandler); | ||
} | ||
|
||
private String getRandomResourceKey() { | ||
var email = "%s@email.com".formatted(UUID.randomUUID().toString().replace("-", "")); | ||
return Hash.hash(email).toString(); | ||
} | ||
|
||
private String getReservationStreamId(String resourceKey) { | ||
return "reservation-%s".formatted(resourceKey); | ||
} | ||
} |