3131import com .google .protobuf .util .Timestamps ;
3232import io .spine .base .Identifier ;
3333import io .spine .server .NodeId ;
34+ import io .spine .server .delivery .PickUpOutcome ;
35+ import io .spine .server .delivery .ShardAlreadyPickedUp ;
3436import io .spine .server .delivery .ShardIndex ;
35- import io .spine .server .delivery .ShardProcessingSession ;
3637import io .spine .server .delivery .ShardSessionRecord ;
3738import io .spine .server .delivery .ShardedWorkRegistry ;
3839import io .spine .server .delivery .ShardedWorkRegistryTest ;
@@ -80,11 +81,11 @@ protected ShardedWorkRegistry registry() {
8081 @ Test
8182 @ DisplayName ("pick up the shard and write a corresponding record to the storage" )
8283 void pickUp () {
83- Optional < ShardProcessingSession > session = registry .pickUp (index , nodeId );
84+ PickUpOutcome outcome = registry .pickUp (index , nodeId );
8485 WorkerId expectedWorker = registry .currentWorkerFor (nodeId );
85- assertThat (session ). isPresent ();
86- assertThat (session . get ()
87- .shardIndex ()).isEqualTo (index );
86+ assertThat (outcome . hasSession ()). isTrue ();
87+ assertThat (outcome . getSession ()
88+ .getIndex ()).isEqualTo (index );
8889
8990 ShardSessionRecord record = readSingleRecord (index );
9091 assertThat (record .getIndex ()).isEqualTo (index );
@@ -95,42 +96,54 @@ void pickUp() {
9596 @ DisplayName ("not be able to pick up the shard if it's already picked up" )
9697 void cannotPickUpIfTaken () {
9798
98- Optional <ShardProcessingSession > session = registry .pickUp (index , nodeId );
99- assertThat (session ).isPresent ();
99+ PickUpOutcome outcome = registry .pickUp (index , nodeId );
100+ assertThat (outcome .hasSession ()).isTrue ();
101+ ShardSessionRecord session = outcome .getSession ();
100102
101- Optional <ShardProcessingSession > sameIdxSameNode = registry .pickUp (index , nodeId );
102- assertThat (sameIdxSameNode ).isEmpty ();
103+ PickUpOutcome sameIdxSameNode = registry .pickUp (index , nodeId );
104+ assertThat (sameIdxSameNode .hasSession ()).isFalse ();
105+ assertThat (sameIdxSameNode .hasAlreadyPicked ()).isTrue ();
103106
104- Optional <ShardProcessingSession > sameIdxAnotherNode = registry .pickUp (index , newNode ());
105- assertThat (sameIdxAnotherNode ).isEmpty ();
107+ ShardAlreadyPickedUp alreadyPicked = sameIdxSameNode .getAlreadyPicked ();
108+ assertThat (alreadyPicked .getWorker ()).isEqualTo (session .getWorker ());
109+
110+ PickUpOutcome sameIdxAnotherNode = registry .pickUp (index , newNode ());
111+ assertThat (sameIdxAnotherNode .hasSession ()).isFalse ();
112+ assertThat (sameIdxAnotherNode .hasAlreadyPicked ()).isTrue ();
113+
114+ ShardAlreadyPickedUp anotherAlreadyPicked = sameIdxAnotherNode .getAlreadyPicked ();
115+ assertThat (anotherAlreadyPicked .getWorker ()).isEqualTo (session .getWorker ());
106116
107117 ShardIndex anotherIdx = newIndex (24 , 100 );
108- Optional <ShardProcessingSession > anotherIdxSameNode = registry .pickUp (anotherIdx , nodeId );
109- assertThat (anotherIdxSameNode ).isPresent ();
118+ PickUpOutcome anotherIdxSameNode = registry .pickUp (anotherIdx , nodeId );
119+ assertThat (anotherIdxSameNode .hasSession ()).isTrue ();
120+ ShardSessionRecord anotherSession = anotherIdxSameNode .getSession ();
121+
122+ PickUpOutcome anotherIdxAnotherNode = registry .pickUp (anotherIdx , newNode ());
123+ assertThat (anotherIdxAnotherNode .hasSession ()).isFalse ();
124+ assertThat (anotherIdxAnotherNode .hasAlreadyPicked ()).isTrue ();
110125
111- Optional <ShardProcessingSession > anotherIdxAnotherNode =
112- registry .pickUp (anotherIdx , newNode ());
113- assertThat (anotherIdxAnotherNode ).isEmpty ();
126+ ShardAlreadyPickedUp oneMoreAnotherPicked = anotherIdxAnotherNode .getAlreadyPicked ();
127+ assertThat (oneMoreAnotherPicked .getWorker ()).isEqualTo (anotherSession .getWorker ());
114128 }
115129
116130 @ Test
117- @ DisplayName ("complete the shard session (once picked up) and make it available for picking up" )
118- void completeSessionAndMakeItAvailable () {
119- Optional < ShardProcessingSession > optional = registry .pickUp (index , nodeId );
120- assertThat (optional ). isPresent ();
131+ @ DisplayName ("release the shard session (once picked up) and make it available for picking up" )
132+ void releaseSessionAndMakeItAvailable () {
133+ PickUpOutcome outcome = registry .pickUp (index , nodeId );
134+ assertThat (outcome . hasSession ()). isTrue ();
121135
122136 Timestamp whenPickedFirst = readSingleRecord (index ).getWhenLastPicked ();
123137
124- DsShardProcessingSession session = (DsShardProcessingSession ) optional .get ();
125- session .complete ();
138+ registry .release (outcome .getSession ());
126139
127140 ShardSessionRecord completedRecord = readSingleRecord (index );
128141 assertThat (completedRecord .hasWorker ()).isFalse ();
129142
130143 NodeId anotherNode = newNode ();
131144 WorkerId anotherWorker = registry .currentWorkerFor (anotherNode );
132- Optional < ShardProcessingSession > anotherOptional = registry .pickUp (index , anotherNode );
133- assertThat (anotherOptional ). isPresent ();
145+ PickUpOutcome anotherOutcome = registry .pickUp (index , anotherNode );
146+ assertThat (anotherOutcome . hasSession ()). isTrue ();
134147
135148 ShardSessionRecord secondSessionRecord = readSingleRecord (index );
136149 assertThat (secondSessionRecord .getWorker ()).isEqualTo (anotherWorker );
@@ -152,7 +165,8 @@ void notAcceptNulls() {
152165 }
153166
154167 private ShardSessionRecord readSingleRecord (ShardIndex index ) {
155- Optional <ShardSessionRecord > record = registry .storage ().read (index );
168+ Optional <ShardSessionRecord > record = registry .storage ()
169+ .read (index );
156170 assertThat (record ).isPresent ();
157171 return record .get ();
158172 }
0 commit comments