Skip to content

Commit

Permalink
mutex algo simulations and tests done 🎉 ✅ 📈
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaushal1011 committed Apr 22, 2024
1 parent d5b7b96 commit 08af805
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 28 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ Install scala, sbt, and java-17
- Uses a custom actor for maintianing shared memory (Peterson Tournament Shared Mem Actor)
- Bakery Algorithm
- Uses a custom actor for maintianing shared memory (Bakery Shared Mem Actor)
- TODO: if node is already requesting, then it should not be able to request again

- Test and Set Lock Algorithm
- Uses a custom actor for maintianing shared memory (Test and Set Shared Mem Actor)
- Test and Test and Set Lock Algorithm
- Uses a custom actor for maintianing shared memory (Test and Test and Set Shared Mem Actor)

### [Simulation Plan](./mutexsimplan.json)

1. Ricart Agarwal
Expand All @@ -47,9 +50,17 @@ Install scala, sbt, and java-17
6. Peterson's Algorithm (Tournament for N Processes) 8 Processes
7. Peterson's Algorithm (Two Process)
8. Bakery Algorithm
9. Test and Set Lock Algorithm
10. Test and Test and Set Lock Algorithm


## Telemetry

### [Mutex](./mutexsimplan.json)

![Mutex Telemetry](./assets/mutexsimplanrun.png)
![Mutex Telemetry](./assets/mutexsimplanrun.png)

## Testing

- Test cases for all algorithms (Integration Testing Based on Log Analysis). Directory [`src/test/scala/com/distcomp/mutex`](src/test/scala/com/distcomp/mutex).
- Test cases for Shared Memory Actors (Unit Testing checks for correctness of shared memory) Directory [`src/test/scala/com/distcomp/sharedmemory`](src/test/scala/com/distcomp/sharedmemory).
Binary file added assets/mutextests.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions mutexsimplan.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@
"additional": 0,
"kill": 1
}
},
{
"dotFilePath": "ExampleNetwork.dot",
"isDirected": true,
"createRing": true,
"createClique": false,
"createBinTree": false,
"enableFailureDetector": false,
"algorithm": "test-and-set",
"additionalParameters": {
"initiators": 5,
"additional": 0,
"kill": 1
}
},{
"dotFilePath": "ExampleNetwork.dot",
"isDirected": true,
"createRing": true,
"createClique": false,
"createBinTree": false,
"enableFailureDetector": false,
"algorithm": "test-and-test-and-set",
"additionalParameters": {
"initiators": 5,
"additional": 0,
"kill": 1
}
}
]
}
25 changes: 0 additions & 25 deletions src/test/scala/com/distcomp/AkkaQuickstartSpec.scala

This file was deleted.

52 changes: 52 additions & 0 deletions src/test/scala/com/distcomp/sharedmemory/BakerySMTest.scala
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
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
49 changes: 49 additions & 0 deletions src/test/scala/com/distcomp/sharedmemory/TASSMTest.scala
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

0 comments on commit 08af805

Please sign in to comment.