Skip to content

Commit

Permalink
#407 Add a test suite for the singal handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
yruslan committed May 16, 2024
1 parent c918d44 commit 07fa7c0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,15 @@ class PipelineStateImpl(implicit conf: Config, notificationBuilder: Notification
}
}

private[state] def getFailureException: Option[Throwable] = failureException

private[state] def setSignalException(ex: Throwable): Unit = {
if (signalException.isEmpty) {
signalException = Option(ex)
}
}

private[state] def getSignalException: Option[Throwable] = signalException
}

object PipelineStateImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PramenSignalHandler(signal: Signal, signalName: String, pipelineState: Pip
override def handle(sig: Signal): Unit = {
val stackTraces = Thread.getAllStackTraces.asScala

val nonDaemonStackTraces = stackTraces.flatMap{ case (t: Thread, s: Array[StackTraceElement]) =>
val nonDaemonStackTraces = stackTraces.flatMap { case (t: Thread, s: Array[StackTraceElement]) =>
if (t.isDaemon) {
None
} else {
Expand All @@ -54,11 +54,9 @@ class PramenSignalHandler(signal: Signal, signalName: String, pipelineState: Pip
}
}

def setOldSignalHandler(signalHandler: SignalHandler): Unit = {
oldHandler = Option(signalHandler)
}
def setOldSignalHandler(signalHandler: SignalHandler): Unit = oldHandler = Option(signalHandler)

def unhandle(): Unit = {
oldHandler.foreach(handler => Signal.handle(signal, handler))
}
def getOldSignalHandler: Option[SignalHandler] = oldHandler

def unhandle(): Unit = oldHandler.foreach(handler => Signal.handle(signal, handler))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2022 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.pramen.core.state

import com.typesafe.config.ConfigFactory
import org.scalatest.wordspec.AnyWordSpec
import sun.misc.{Signal, SignalHandler}

class PramenSignalHandlerSuite extends AnyWordSpec {
private val conf = ConfigFactory.parseString("""pramen.pipeline.name = "test"""")
.withFallback(ConfigFactory.load())

"handle" should {
"run the handler and the old handler" in {
val notificationBuilder = new NotificationBuilderImpl
val stateImpl = new PipelineStateImpl()(conf, notificationBuilder)
var reached = false

val oldSignalHandlerMock = new SignalHandler {
override def handle(sig: Signal): Unit = reached = true
}

val signalHandler = new PramenSignalHandler(new Signal("PIPE"), "Interrupt", stateImpl)

signalHandler.setOldSignalHandler(oldSignalHandlerMock)
assert(signalHandler.getOldSignalHandler.get == oldSignalHandlerMock)
assert(!reached)
Signal.handle(new Signal("PIPE"), signalHandler)
signalHandler.handle(null)
signalHandler.unhandle()
assert(reached)
assert(stateImpl.getSignalException.nonEmpty)
assert(stateImpl.getFailureException.isEmpty)
}
}

}

0 comments on commit 07fa7c0

Please sign in to comment.