Skip to content

Instantly share code, notes, and snippets.

@polymorphic
polymorphic / ObjectProtected.scala
Created May 4, 2015 16:44
Snippet illustrating how the object-protected modifier could cause the Scala compiler to generate code that throws ClassFormatError.
/*
Code for illustrative purposes only, to demonstrate a particular behavior.
I already know that it doesn't run (throws ClassFormatError), and I wouldn't
write it this way to begin with :)
*/
package eg
class Base() {
def foo = "bar"
@polymorphic
polymorphic / AgentServerSuite.scala
Created February 18, 2015 17:06
FunSuite w/ Akka TestKit
import akka.actor.ActorSystem
import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit}
import com.typesafe.config.ConfigFactory
import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuiteLike
class AgentServerSuite extends TestKit(ActorSystem("AgentServerSuite"))
with FunSuiteLike
with DefaultTimeout
with ImplicitSender
@polymorphic
polymorphic / ITConfig.scala
Created February 4, 2015 21:48
Separate integration tests in sbt
/*
Separate integration and unit tests (put in project/ITConfic.scala)
See: http://www.scala-sbt.org/release/docs/Testing.html#Integration+Tests
*/
object ITConfig extends Build {
lazy val root =
Project("service-agent", file("."))
.configs(IntegrationTest)
.settings(Defaults.itSettings: _*)
@polymorphic
polymorphic / StingrayControl.scala
Created February 3, 2015 17:49
Disable checks when using SSL w/ spray.io
/*
spray.io custom SSLContext to bypass security checks (when testing, etc.); see http://spray.io/documentation/1.2.2/spray-can/http-server/#ssl-support
Bringing it into scope removes errors
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
*/
implicit val trustfulSslContext: SSLContext = {
object NoCheckX509TrustManager extends X509TrustManager {
override def checkClientTrusted(chain: Array[X509Certificate], authType: String) = ()
override def checkServerTrusted(chain: Array[X509Certificate], authType: String) = ()
@polymorphic
polymorphic / ElapsedTimeMeasurement.scala
Created November 25, 2014 19:19
Scala partially-applied function for measuring elapsed times
import scala.concurrent.duration._
object Measurements {
/*
Partially-aplied function to measure elapsed times.
Usage:
val myTimer = buildTimer // start the timer
// code under measurement
val elapsedTime1 = myTimer() // record elapsed time 1, leave the timer running
@polymorphic
polymorphic / BaseClient.scala
Created December 5, 2013 01:30
Symbol instead of String
val timeout1 = Promise[Any]()
system.scheduler.scheduleOnce(callTimeout(cnt))(timeout1.success('timeout))
@polymorphic
polymorphic / XymonReporterTest.scala
Created October 9, 2013 00:26
Delete directory (recursive)
val path = FileSystems.getDefault.getPath(dirName)
if (Files.exists(path) && Files.isDirectory(path)) {
Files.walkFileTree(path, new FileVisitor[Path] {
def visitFileFailed(file: Path, exc: IOException) = FileVisitResult.CONTINUE
def visitFile(file: Path, attrs: BasicFileAttributes) = {
Files.delete(file)
FileVisitResult.CONTINUE
}
@polymorphic
polymorphic / ProxyCoordinator.scala
Created September 5, 2013 17:17
Akka 2.2.0 Props for new instance w/ inline override
cmdbTopology = context.actorOf(Props(new CmdbTopology {
override val supervisorStrategy = OneForOneStrategy() {
case nai: NotApplicableToItem => Resume // ensure recipe exception do not stop the actor
}
}).withDispatcher("topology-dispatcher"), "cmdb-topology")
@polymorphic
polymorphic / git+nova.complete
Created June 1, 2012 05:48
tcsh completion: git and nova
complete git p/1/"(add bisect branch checkout clone commit diff fetch grep init log merge mv pull push rebase reset rm show status tag)"/
complete nova p/1/"(absolute-limits actions add-fixed-ip add-floating-ip aggregate-add-host aggregate-create aggregate-delete aggregate-details aggregate-list aggregate-remove-host aggregate-set-metadata aggregate-update boot credentials delete describe-resource diagnostics dns-create dns-create-private-domain dns-create-public-domain dns-delete dns-delete-domain dns-domains dns-list endpoints flavor-create flavor-delete flavor-list floating-ip-create floating-ip-delete floating-ip-list floating-ip-pool-list get-vnc-console image-create image-delete image-list image-meta image-show keypair-add keypair-delete keypair-list list live-migration meta migrate pause rate-limits reboot rebuild remove-fixed-ip remove-floating-ip rename rescue resize resize-confirm resize-revert resume root-password secgroup-add-group-rule secgroup-add-rule secgroup-create secgroup-delete sec