- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1k
 
Convert rediscala test to scala #12616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| plugins { | ||
| id("otel.javaagent-instrumentation") | ||
| id("otel.scala-conventions") | ||
| } | ||
| 
     | 
||
| muzzle { | ||
| 
          
            
          
           | 
    ||
        
          
  
    
      
          
            145 changes: 0 additions & 145 deletions
          
          145 
        
  instrumentation/rediscala-1.8/javaagent/src/test/groovy/RediscalaClientTest.groovy
  
  
      
      
   
        
      
      
    This file was deleted.
      
      Oops, something went wrong.
      
    
  
        
          
  
    
      
          
            189 changes: 189 additions & 0 deletions
          
          189 
        
  ...test/scala/io/opentelemetry/javaagent/instrumentation/rediscala/RediscalaClientTest.scala
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,189 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package rediscala | ||
| 
     | 
||
| import io.opentelemetry.api.trace.SpanKind.CLIENT | ||
| import io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil | ||
| import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension | ||
| import io.opentelemetry.instrumentation.testing.util.ThrowingSupplier | ||
| import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo | ||
| import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert} | ||
| import io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION | ||
| import io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM | ||
| import io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemIncubatingValues.REDIS | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.{AfterAll, BeforeAll, Test, TestInstance} | ||
| import org.junit.jupiter.api.extension.RegisterExtension | ||
| import org.testcontainers.containers.GenericContainer | ||
| import redis.{RedisClient, RedisDispatcher} | ||
| 
     | 
||
| import java.util.function.Consumer | ||
| import scala.concurrent.duration.Duration | ||
| import scala.concurrent.{Await, Future} | ||
| 
     | 
||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class RediscalaClientTest { | ||
| 
     | 
||
| @RegisterExtension val testing = AgentInstrumentationExtension.create | ||
| 
     | 
||
| var system: Object = null | ||
| var redisServer: GenericContainer[_] = null | ||
| var redisClient: RedisClient = null | ||
| 
     | 
||
| @BeforeAll | ||
| def setUp(): Unit = { | ||
| redisServer = | ||
| new GenericContainer("redis:6.2.3-alpine").withExposedPorts(6379) | ||
| redisServer.start() | ||
| 
     | 
||
| val host: String = redisServer.getHost | ||
| val port: Integer = redisServer.getMappedPort(6379) | ||
| 
     | 
||
| try { | ||
| val clazz = Class.forName("akka.actor.ActorSystem") | ||
| system = clazz.getMethod("create").invoke(null) | ||
| } catch { | ||
| case _: ClassNotFoundException => | ||
| val clazz = Class.forName("org.apache.pekko.actor.ActorSystem") | ||
| system = clazz.getMethod("create").invoke(null) | ||
| } | ||
| 
     | 
||
| try { | ||
| // latest RedisClient constructor takes username as argument | ||
| classOf[RedisClient].getMethod("username") | ||
| redisClient = classOf[RedisClient] | ||
| .getConstructors()(0) | ||
| .newInstance( | ||
| host, | ||
| port, | ||
| Option.apply(null), | ||
| Option.apply(null), | ||
| Option.apply(null), | ||
| "RedisClient", | ||
| Option.apply(null), | ||
| system, | ||
| RedisDispatcher("rediscala.rediscala-client-worker-dispatcher") | ||
| ) | ||
| .asInstanceOf[RedisClient] | ||
| } catch { | ||
| case _: Exception => | ||
| redisClient = classOf[RedisClient] | ||
| .getConstructors()(0) | ||
| .newInstance( | ||
| host, | ||
| port, | ||
| Option.apply(null), | ||
| Option.apply(null), | ||
| "RedisClient", | ||
| Option.apply(null), | ||
| system, | ||
| RedisDispatcher("rediscala.rediscala-client-worker-dispatcher") | ||
| ) | ||
| .asInstanceOf[RedisClient] | ||
| } | ||
| } | ||
| 
     | 
||
| @AfterAll | ||
| def tearDown(): Unit = { | ||
| if (system != null) { | ||
| system.getClass.getMethod("terminate").invoke(system) | ||
| } | ||
| redisServer.stop() | ||
| } | ||
| 
     | 
||
| @Test def testSetCommand(): Unit = { | ||
| val value = testing.runWithSpan( | ||
| "parent", | ||
| new ThrowingSupplier[Future[Boolean], Exception] { | ||
| override def get(): Future[Boolean] = { | ||
| redisClient.set("foo", "bar") | ||
| } | ||
| } | ||
| ) | ||
| 
     | 
||
| assertThat(Await.result(value, Duration.apply("3 second"))).isTrue | ||
| testing.waitAndAssertTraces(new Consumer[TraceAssert] { | ||
| override def accept(trace: TraceAssert): Unit = | ||
| trace.hasSpansSatisfyingExactly( | ||
| new Consumer[SpanDataAssert] { | ||
| override def accept(span: SpanDataAssert): Unit = { | ||
| span.hasName("parent").hasNoParent | ||
| } | ||
| }, | ||
| new Consumer[SpanDataAssert] { | ||
| override def accept(span: SpanDataAssert): Unit = { | ||
| span | ||
| .hasName("SET") | ||
| .hasKind(CLIENT) | ||
| .hasParent(trace.getSpan(0)) | ||
| .hasAttributesSatisfying( | ||
| equalTo(DB_SYSTEM, REDIS), | ||
| equalTo(SemconvStabilityUtil.maybeStable(DB_OPERATION), "SET") | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
| }) | ||
| } | ||
| 
     | 
||
| @Test def testGetCommand(): Unit = { | ||
| val (write, value) = testing.runWithSpan( | ||
| "parent", | ||
| new ThrowingSupplier[ | ||
| (Future[Boolean], Future[Option[String]]), | ||
| Exception | ||
| ] { | ||
| override def get(): (Future[Boolean], Future[Option[String]]) = { | ||
| val write = redisClient.set("bar", "baz") | ||
| val value = redisClient.get[String]("bar") | ||
| (write, value) | ||
| } | ||
| } | ||
| ) | ||
| 
     | 
||
| assertThat(Await.result(write, Duration.apply("3 second"))).isTrue | ||
| assertThat( | ||
| Await | ||
| .result(value, Duration.apply("3 second")) | ||
| .get | ||
| ).isEqualTo("baz") | ||
| 
     | 
||
| testing.waitAndAssertTraces(new Consumer[TraceAssert] { | ||
| override def accept(trace: TraceAssert): Unit = | ||
| trace.hasSpansSatisfyingExactly( | ||
| new Consumer[SpanDataAssert] { | ||
| override def accept(span: SpanDataAssert): Unit = { | ||
| span.hasName("parent").hasNoParent | ||
| } | ||
| }, | ||
| new Consumer[SpanDataAssert] { | ||
| override def accept(span: SpanDataAssert): Unit = { | ||
| span | ||
| .hasName("SET") | ||
| .hasKind(CLIENT) | ||
| .hasParent(trace.getSpan(0)) | ||
| .hasAttributesSatisfying( | ||
| equalTo(DB_SYSTEM, REDIS), | ||
| equalTo(SemconvStabilityUtil.maybeStable(DB_OPERATION), "SET") | ||
| ) | ||
| } | ||
| }, | ||
| new Consumer[SpanDataAssert] { | ||
| override def accept(span: SpanDataAssert): Unit = { | ||
| span | ||
| .hasName("GET") | ||
| .hasKind(CLIENT) | ||
| .hasParent(trace.getSpan(0)) | ||
| .hasAttributesSatisfying( | ||
| equalTo(DB_SYSTEM, REDIS), | ||
| equalTo(SemconvStabilityUtil.maybeStable(DB_OPERATION), "GET") | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
| }) | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.