-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mutex algo simulations and tests done 🎉 ✅ 📈
- Loading branch information
1 parent
d5b7b96
commit 08af805
Showing
7 changed files
with
183 additions
and
28 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
src/test/scala/com/distcomp/sharedmemory/BakerySMTest.scala
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,52 @@ | ||
//#full-example | ||
package com.distcomp.sharedmemory | ||
|
||
import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
import com.distcomp.common.BakeryProtocol._ | ||
import com.distcomp.common.Message | ||
import com.distcomp.utils.DummyNodeActor | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
//#definition | ||
class BakerySMTest extends ScalaTestWithActorTestKit with AnyWordSpecLike { | ||
//#definition | ||
|
||
"A BakerySharedMemoryActor" must { | ||
val replyProbe = createTestProbe[Message]() | ||
|
||
val nodeIds: List[Int] = List(1,2,3,4,5,6) | ||
|
||
val nodeSet = nodeIds.map(id => spawn(DummyNodeActor(), s"Dummy Node $id")).toSet | ||
|
||
val finalSet = nodeSet + replyProbe.ref | ||
|
||
val underTest = spawn(BakerySharedMemActor(finalSet)) | ||
|
||
//#test | ||
"must read numbers successfully" in { | ||
underTest ! ReadNumbers(replyProbe.ref) | ||
replyProbe.expectMessage(ReadNumbersReply(finalSet.map(node => node -> 0).toMap)) | ||
} | ||
|
||
"must set choosing, numbers and read responses properly" in { | ||
underTest ! SetChoosing(replyProbe.ref, true) | ||
|
||
replyProbe.expectMessage(SetChoosingReply(true)) | ||
|
||
underTest ! SetNumber(replyProbe.ref, 3) | ||
|
||
underTest ! GetChoosingAndNumber(replyProbe.ref) | ||
|
||
val choosingBefore = finalSet.map(node => node -> false).toMap | ||
val numbersBefore = finalSet.map(node => node -> 0).toMap | ||
|
||
val choosingAfter = choosingBefore.updated(replyProbe.ref, true) | ||
val numbersAfter = numbersBefore.updated(replyProbe.ref, 3) | ||
|
||
replyProbe.expectMessage(GetChoosingAndNumberReply(choosingAfter, numbersAfter)) | ||
|
||
} | ||
//#test | ||
} | ||
} | ||
//#full-example |
41 changes: 41 additions & 0 deletions
41
src/test/scala/com/distcomp/sharedmemory/PetersonTournamentSMTest.scala
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,41 @@ | ||
//#full-example | ||
package com.distcomp.sharedmemory | ||
|
||
import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
import com.distcomp.common.Message | ||
import com.distcomp.common.PetersonTournamentProtocol._ | ||
import com.distcomp.utils.DummyNodeActor | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
//#definition | ||
class PetersonTournamentSMTest extends ScalaTestWithActorTestKit with AnyWordSpecLike { | ||
//#definition | ||
|
||
"A PetersonTournamentSharedMemoryActor" must { | ||
val replyProbe = createTestProbe[Message]() | ||
|
||
val nodeIds: List[Int] = List(1,2,3,4,5,6) | ||
|
||
val nodeSet = nodeIds.map(id => spawn(DummyNodeActor(), s"Dummy Node $id")).toSet | ||
|
||
val underTest = spawn(PetersonTournamentSharedMemActor(nodeSet)) | ||
|
||
//#test | ||
"must reply on read flag and turn" in { | ||
underTest ! ReadFlagAndTurnTournament(replyProbe.ref, 3, 0) | ||
replyProbe.expectMessage(ReadFlagAndTurnTournamentReply(false, -1, 3)) | ||
} | ||
|
||
"must set flag and turn for a node" in { | ||
underTest ! SetFlagTournament(3,0, true) | ||
underTest ! SetTurnTournament(3,0) | ||
underTest ! ReadFlagAndTurnTournament(replyProbe.ref, 3, 0) | ||
|
||
replyProbe.expectMessage(ReadFlagAndTurnTournamentReply(true, 0, 3)) | ||
|
||
|
||
} | ||
//#test | ||
} | ||
} | ||
//#full-example |
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,49 @@ | ||
//#full-example | ||
package com.distcomp.sharedmemory | ||
|
||
import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
import com.distcomp.common.Message | ||
import com.distcomp.common.PetersonTwoProcess._ | ||
import com.distcomp.common.TestAndSetSharedMemProtocol.{ReadLockRequest, ReadLockResponse, SetLockRequest, SetLockResponse, UnlockRequest} | ||
import com.distcomp.utils.DummyNodeActor | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
//#definition | ||
class TASSMTest extends ScalaTestWithActorTestKit with AnyWordSpecLike { | ||
//#definition | ||
|
||
|
||
|
||
"A TestAndSetSharedMemoryActor" must { | ||
val replyProbe = createTestProbe[Message]() | ||
|
||
val underTest = spawn(TestAndSetSharedMemActor()) | ||
|
||
//#test | ||
"must read lock request properly" in { | ||
underTest ! ReadLockRequest(replyProbe.ref) | ||
replyProbe.expectMessage(ReadLockResponse(underTest,false)) | ||
} | ||
|
||
"must set lock properly" in { | ||
underTest ! SetLockRequest(replyProbe.ref) | ||
// lock was false, so the response should be false but lock is set to true | ||
replyProbe.expectMessage(SetLockResponse(false)) | ||
|
||
underTest ! ReadLockRequest(replyProbe.ref) | ||
replyProbe.expectMessage(ReadLockResponse(underTest,true)) | ||
} | ||
|
||
"must unlock and read lock request properly" in { | ||
underTest ! UnlockRequest | ||
underTest ! ReadLockRequest(replyProbe.ref) | ||
replyProbe.expectMessage(ReadLockResponse(underTest,false)) | ||
} | ||
|
||
|
||
|
||
//#test | ||
} | ||
|
||
} | ||
//#full-example |