-
Notifications
You must be signed in to change notification settings - Fork 2
/
BigtableTestSuite.scala
88 lines (70 loc) · 2.81 KB
/
BigtableTestSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.steveniemitz.littletable
import com.google.cloud.bigtable.config.BigtableOptions
import com.google.cloud.bigtable.grpc.BigtableDataClient
import com.google.cloud.bigtable.grpc.BigtableSession
import com.google.protobuf.ByteString
import io.grpc.inprocess.InProcessChannelBuilder
import io.grpc.inprocess.InProcessServerBuilder
import io.grpc.Channel
import io.grpc.ManagedChannel
import io.grpc.Server
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import org.scalatest.BeforeAndAfterEach
import org.scalatest.Inspectors
import scala.jdk.CollectionConverters._
private[littletable] trait BigtableTestSuite
extends FunTestSuite
with Inspectors
with BeforeAndAfterEach {
implicit class ByteStringTransformer(sc: StringContext) {
def bs(args: Any*): ByteString = ByteString.copyFromUtf8(sc.s(args: _*))
}
protected def bs(chars: Char*): ByteString =
ByteString.copyFrom(chars.map(_.toByte).toArray)
private var server: Server = _
private var dataService: BigtableDataService = _
private var tableAdminService: BigtableAdminService = _
private var _tableData: ConcurrentHashMap[String, Table] = _
private var _session: BigtableSession = _
private var _channel: ManagedChannel = _
protected def inProcessChannel: Channel = _channel
protected def tableData: Map[String, Table] = _tableData.asScala.toMap
protected def session: BigtableSession = _session
protected def dataClient: BigtableDataClient = _session.getDataClient
protected final val TestTableName = "projects/derp/instances/derp/tables/test"
protected def testTable: Table = _tableData.get(TestTableName)
protected def utf8(value: String): ByteString = ByteString.copyFromUtf8(value)
override def beforeEach(): Unit = {
System.setProperty("BIGTABLE_SESSION_SKIP_WARMUP", "true")
_tableData = new ConcurrentHashMap[String, Table]()
_tableData.put(TestTableName, new Table(Set("f1")))
dataService = new BigtableDataService(_tableData)
tableAdminService = new BigtableAdminService(_tableData)
val uniqueName = InProcessServerBuilder.generateName
_channel = InProcessChannelBuilder
.forName(uniqueName)
.directExecutor()
.build()
server = InProcessServerBuilder
.forName(uniqueName)
.directExecutor
.addService(dataService)
.addService(tableAdminService)
.build
.start
val bigtableOptions = new BigtableOptions.Builder()
.setProjectId("derp")
.setInstanceId("derp")
.setUserAgent("derp")
.enableEmulator("localhost", 12345)
.build()
_session = new InProcessBigtableSession(bigtableOptions, uniqueName)
}
override def afterEach(): Unit = {
_channel.shutdownNow()
_channel.awaitTermination(100, TimeUnit.DAYS)
server.shutdown()
server.awaitTermination()
}
}