This is an automated email from the ASF dual-hosted git repository.

style95 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new c507069d1 Do not put data to ETCD when no date is changed. (#5291)
c507069d1 is described below

commit c507069d1b3983bff20c3d9e306cd3694e6c6a3c
Author: Dominic Kim <[email protected]>
AuthorDate: Wed Jul 27 10:21:58 2022 +0900

    Do not put data to ETCD when no date is changed. (#5291)
    
    * Do not put data to ETCD when no date is changed.
    
    * Update the hashcode calculation.
    
    * Take dedicated namespace into account when calculating the hash code.
    
    * Apply the comment.
    
    * Apply the comment.
---
 .../apache/openwhisk/core/connector/Message.scala  | 25 +++++++
 .../openwhisk/core/scheduler/Scheduler.scala       | 15 ++++
 .../core/connector/test/MessageTests.scala         | 87 ++++++++++++++++++++++
 3 files changed, 127 insertions(+)

diff --git 
a/common/scala/src/main/scala/org/apache/openwhisk/core/connector/Message.scala 
b/common/scala/src/main/scala/org/apache/openwhisk/core/connector/Message.scala
index 9636769b2..11749bed0 100644
--- 
a/common/scala/src/main/scala/org/apache/openwhisk/core/connector/Message.scala
+++ 
b/common/scala/src/main/scala/org/apache/openwhisk/core/connector/Message.scala
@@ -470,6 +470,31 @@ case class InvokerResourceMessage(status: String,
    * Serializes message to string. Must be idempotent.
    */
   override def serialize: String = 
InvokerResourceMessage.serdes.write(this).compactPrint
+
+  override def equals(that: Any): Boolean =
+    that match {
+      case that: InvokerResourceMessage =>
+        this.status == that.status &&
+          this.freeMemory == that.freeMemory &&
+          this.busyMemory == that.busyMemory &&
+          this.inProgressMemory == that.inProgressMemory &&
+          this.tags.toSet == that.tags.toSet &&
+          this.dedicatedNamespaces.toSet == that.dedicatedNamespaces.toSet
+
+      case _ => false
+    }
+
+  override def hashCode: Int = {
+    var result = 1
+    val prime = 31
+    result = prime * result + status.hashCode()
+    result = prime * result + freeMemory.hashCode()
+    result = prime * result + busyMemory.hashCode()
+    result = prime * result + inProgressMemory.hashCode()
+    result = prime * result + tags.hashCode()
+    result = prime * result + dedicatedNamespaces.hashCode()
+    result
+  }
 }
 
 object InvokerResourceMessage extends DefaultJsonProtocol {
diff --git 
a/core/scheduler/src/main/scala/org/apache/openwhisk/core/scheduler/Scheduler.scala
 
b/core/scheduler/src/main/scala/org/apache/openwhisk/core/scheduler/Scheduler.scala
index 6591d2e47..128c004ef 100644
--- 
a/core/scheduler/src/main/scala/org/apache/openwhisk/core/scheduler/Scheduler.scala
+++ 
b/core/scheduler/src/main/scala/org/apache/openwhisk/core/scheduler/Scheduler.scala
@@ -389,6 +389,21 @@ case class SchedulerStates(sid: SchedulerInstanceId, 
queueSize: Int, endpoints:
   def getSchedulerId(): SchedulerInstanceId = sid
 
   def serialize = SchedulerStates.serdes.write(this).compactPrint
+
+  override def equals(that: Any): Boolean =
+    that match {
+      case that: SchedulerStates => {
+        this.queueSize == that.queueSize
+      }
+      case _ => false
+    }
+
+  override def hashCode: Int = {
+    var result = 1
+    val prime = 31
+    result = prime * result + queueSize.hashCode()
+    result
+  }
 }
 
 object SchedulerStates extends DefaultJsonProtocol {
diff --git 
a/tests/src/test/scala/org/apache/openwhisk/core/connector/test/MessageTests.scala
 
b/tests/src/test/scala/org/apache/openwhisk/core/connector/test/MessageTests.scala
new file mode 100644
index 000000000..97d4d1984
--- /dev/null
+++ 
b/tests/src/test/scala/org/apache/openwhisk/core/connector/test/MessageTests.scala
@@ -0,0 +1,87 @@
+package org.apache.openwhisk.core.connector.test
+
+import akka.actor.ActorSystem
+import akka.testkit.TestKit
+import org.apache.openwhisk.common.InvokerState.{Healthy, Unhealthy}
+import org.apache.openwhisk.core.connector.InvokerResourceMessage
+import org.apache.openwhisk.core.entity.SchedulerInstanceId
+import org.apache.openwhisk.core.scheduler.{SchedulerEndpoints, 
SchedulerStates}
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+import org.scalatest.{FlatSpecLike, Matchers}
+
+@RunWith(classOf[JUnitRunner])
+class MessageTests extends TestKit(ActorSystem("Message")) with FlatSpecLike 
with Matchers {
+  behavior of "Message"
+
+  it should "be able to compare the InvokerResourceMessage" in {
+    val msg1 = InvokerResourceMessage(Unhealthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+    val msg2 = InvokerResourceMessage(Unhealthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+
+    msg1 == msg2 shouldBe true
+  }
+
+  it should "be different when the state of InvokerResourceMessage is 
different" in {
+    val msg1 = InvokerResourceMessage(Unhealthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+    val msg2 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be different when the free memory of InvokerResourceMessage is 
different" in {
+    val msg1 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+    val msg2 = InvokerResourceMessage(Healthy.asString, 2048L, 0, 0, 
Seq.empty, Seq.empty)
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be different when the busy memory of InvokerResourceMessage is 
different" in {
+    val msg1 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+    val msg2 = InvokerResourceMessage(Healthy.asString, 1024L, 1024L, 0, 
Seq.empty, Seq.empty)
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be different when the in-progress memory of 
InvokerResourceMessage is different" in {
+    val msg1 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq.empty, Seq.empty)
+    val msg2 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 1024L, 
Seq.empty, Seq.empty)
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be different when the tags of InvokerResourceMessage is 
different" in {
+    val msg1 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq("tag1"), Seq.empty)
+    val msg2 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq("tag1", "tag2"), Seq.empty)
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be different when the dedicated namespaces of 
InvokerResourceMessage is different" in {
+    val msg1 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq.empty, Seq("ns1"))
+    val msg2 = InvokerResourceMessage(Healthy.asString, 1024L, 0, 0, 
Seq.empty, Seq("ns2"))
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be able to compare the SchedulerStates" in {
+    val msg1 = SchedulerStates(SchedulerInstanceId("0"), queueSize = 0, 
SchedulerEndpoints("10.10.10.10", 1234, 1234))
+    val msg2 = SchedulerStates(SchedulerInstanceId("0"), queueSize = 0, 
SchedulerEndpoints("10.10.10.10", 1234, 1234))
+
+    msg1 == msg2 shouldBe true
+  }
+
+  it should "be different when the queue size of SchedulerStates is different" 
in {
+    val msg1 = SchedulerStates(SchedulerInstanceId("0"), queueSize = 20, 
SchedulerEndpoints("10.10.10.10", 1234, 1234))
+    val msg2 = SchedulerStates(SchedulerInstanceId("0"), queueSize = 10, 
SchedulerEndpoints("10.10.10.10", 1234, 1234))
+
+    msg1 != msg2 shouldBe true
+  }
+
+  it should "be not different when other than the queue size of 
SchedulerStates is different" in {
+    // only the queue size matter
+    val msg1 = SchedulerStates(SchedulerInstanceId("0"), queueSize = 20, 
SchedulerEndpoints("10.10.10.10", 1234, 1234))
+    val msg2 = SchedulerStates(SchedulerInstanceId("1"), queueSize = 20, 
SchedulerEndpoints("10.10.10.20", 5678, 5678))
+
+    msg1 == msg2 shouldBe true
+  }
+}

Reply via email to