From 5345687d23d1c23cce6d75e8939b856ff9484a06 Mon Sep 17 00:00:00 2001 From: Kent Yao Date: Sat, 23 Jun 2018 00:07:40 +0800 Subject: [PATCH] add ut --- .../yaooqinn/kyuubi/ui/ExecutionInfo.scala | 4 +- .../kyuubi/ui/KyuubiServerMonitor.scala | 14 ++--- .../scala/yaooqinn/kyuubi/ui/package.scala | 24 --------- .../kyuubi/ui/ExecutionInfoSuite.scala | 42 +++++++++++++++ .../kyuubi/ui/KyuubiServerMonitorSuite.scala | 51 +++++++++++++++++++ 5 files changed, 102 insertions(+), 33 deletions(-) delete mode 100644 kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/package.scala create mode 100644 kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/ExecutionInfoSuite.scala create mode 100644 kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitorSuite.scala diff --git a/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/ExecutionInfo.scala b/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/ExecutionInfo.scala index c504b051c..2833d74ab 100644 --- a/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/ExecutionInfo.scala +++ b/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/ExecutionInfo.scala @@ -6,7 +6,7 @@ * (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 + * 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, @@ -23,7 +23,7 @@ class ExecutionInfo( val statement: String, val sessionId: String, val startTimestamp: Long, - val userName: User) { + val userName: String) { var finishTimestamp: Long = 0L var executePlan: String = "" var detail: String = "" diff --git a/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitor.scala b/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitor.scala index d0c7cfda8..05adf4d81 100644 --- a/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitor.scala +++ b/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitor.scala @@ -6,7 +6,7 @@ * (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 + * 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, @@ -25,23 +25,23 @@ import org.apache.spark.ui.KyuubiServerTab object KyuubiServerMonitor { - private[this] val uiTabs = new HashMap[User, KyuubiServerTab]() + private[this] val uiTabs = new HashMap[String, KyuubiServerTab]() - private[this] val listeners = new HashMap[User, KyuubiServerListener]() + private[this] val listeners = new HashMap[String, KyuubiServerListener]() - def setListener(user: User, sparkListener: KyuubiServerListener): Unit = { + def setListener(user: String, sparkListener: KyuubiServerListener): Unit = { listeners.put(user, sparkListener) } - def getListener(user: User): Option[KyuubiServerListener] = { + def getListener(user: String): Option[KyuubiServerListener] = { listeners.get(user) } - def addUITab(user: User, ui: KyuubiServerTab): Unit = { + def addUITab(user: String, ui: KyuubiServerTab): Unit = { uiTabs.put(user, ui) } - def detachUITab(user: User): Unit = { + def detachUITab(user: String): Unit = { listeners.remove(user) uiTabs.get(user).foreach(_.detach()) } diff --git a/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/package.scala b/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/package.scala deleted file mode 100644 index 47d2f91f3..000000000 --- a/kyuubi-server/src/main/scala/yaooqinn/kyuubi/ui/package.scala +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 yaooqinn.kyuubi - -package object ui { - - type User = String - -} diff --git a/kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/ExecutionInfoSuite.scala b/kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/ExecutionInfoSuite.scala new file mode 100644 index 000000000..9fbd76081 --- /dev/null +++ b/kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/ExecutionInfoSuite.scala @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 yaooqinn.kyuubi.ui + +import java.util.UUID + +import org.apache.spark.{KyuubiSparkUtil, SparkFunSuite} + +class ExecutionInfoSuite extends SparkFunSuite { + + test("execution info") { + val statement = "show tables" + val sessionId = UUID.randomUUID().toString + val startTimeStamp = System.currentTimeMillis() - 1000L + val user = KyuubiSparkUtil.getCurrentUserName() + val info = new ExecutionInfo(statement, sessionId, startTimeStamp, user) + assert(info.finishTimestamp === 0L) + assert(info.executePlan === "") + assert(info.detail === "") + assert(info.state === ExecutionState.STARTED) + assert(info.groupId === "") + assert(info.jobId.isEmpty) + assert(info.totalTime !== info.finishTimestamp - startTimeStamp) + info.finishTimestamp = System.currentTimeMillis() + assert(info.totalTime === info.finishTimestamp - startTimeStamp) + } +} diff --git a/kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitorSuite.scala b/kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitorSuite.scala new file mode 100644 index 000000000..b68737dfd --- /dev/null +++ b/kyuubi-server/src/test/scala/yaooqinn/kyuubi/ui/KyuubiServerMonitorSuite.scala @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 yaooqinn.kyuubi.ui + +import org.apache.spark.{KyuubiSparkUtil, SparkConf, SparkContext, SparkFunSuite} +import org.apache.spark.ui.KyuubiServerTab + +class KyuubiServerMonitorSuite extends SparkFunSuite { + val conf = new SparkConf(loadDefaults = true).setAppName("monitor").setMaster("local") + + var sc: SparkContext = _ + + override protected def beforeAll(): Unit = { + sc = new SparkContext(conf) + } + + protected override def afterAll(): Unit = { + if (sc != null) { + sc.stop() + } + } + + test("kyuubi server monitor") { + val li = new KyuubiServerListener(conf) + val user = KyuubiSparkUtil.getCurrentUserName() + KyuubiServerMonitor.setListener(user, li) + val liOp = KyuubiServerMonitor.getListener(user) + assert(liOp.get === li) + assert(KyuubiServerMonitor.getListener("fake").isEmpty) + val tab = new KyuubiServerTab(user, sc) + KyuubiServerMonitor.addUITab(user, tab) + KyuubiServerMonitor.detachUITab(user) + KyuubiServerMonitor.detachAllUITabs() + } + +}