|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.openwhisk.core.loadBalancer.test |
| 19 | + |
| 20 | +import akka.actor.{ActorRef, ActorRefFactory, ActorSystem} |
| 21 | +import akka.stream.ActorMaterializer |
| 22 | +import akka.testkit.TestProbe |
| 23 | +import common.StreamLogging |
| 24 | +import org.apache.kafka.clients.producer.RecordMetadata |
| 25 | +import org.apache.kafka.common.TopicPartition |
| 26 | +import org.apache.openwhisk.common.{Logging, TransactionId} |
| 27 | +import org.apache.openwhisk.core.WhiskConfig |
| 28 | +import org.apache.openwhisk.core.connector._ |
| 29 | +import org.apache.openwhisk.core.entity.size._ |
| 30 | +import org.apache.openwhisk.core.entity.test.ExecHelpers |
| 31 | +import org.apache.openwhisk.core.entity._ |
| 32 | +import org.apache.openwhisk.core.loadBalancer._ |
| 33 | +import org.junit.runner.RunWith |
| 34 | +import org.scalamock.scalatest.MockFactory |
| 35 | +import org.scalatest.{FlatSpec, Matchers} |
| 36 | +import org.scalatest.junit.JUnitRunner |
| 37 | + |
| 38 | +import scala.collection.immutable.Map |
| 39 | +import scala.concurrent.{Await, Future} |
| 40 | +import scala.concurrent.duration._ |
| 41 | + |
| 42 | +/** |
| 43 | + * Unit tests for the MuxBalancer object. |
| 44 | + * |
| 45 | + */ |
| 46 | +@RunWith(classOf[JUnitRunner]) |
| 47 | +class MuxBalancerTests extends FlatSpec with Matchers with StreamLogging with ExecHelpers with MockFactory { |
| 48 | + behavior of "Mux Balancer" |
| 49 | + |
| 50 | + def lbConfig(activationStrategy: ActivationStrategy) = |
| 51 | + ShardingContainerPoolBalancerConfig(activationStrategy, 1.0 - 0.5, 0.5, 1, 1.minute) |
| 52 | + |
| 53 | + implicit val transId: TransactionId = TransactionId.testing |
| 54 | + |
| 55 | + val feedProbe = new FeedFactory { |
| 56 | + def createFeed(f: ActorRefFactory, m: MessagingProvider, p: (Array[Byte]) => Future[Unit]) = |
| 57 | + TestProbe().testActor |
| 58 | + |
| 59 | + } |
| 60 | + val invokerPoolProbe = new InvokerPoolFactory { |
| 61 | + override def createInvokerPool( |
| 62 | + actorRefFactory: ActorRefFactory, |
| 63 | + messagingProvider: MessagingProvider, |
| 64 | + messagingProducer: MessageProducer, |
| 65 | + sendActivationToInvoker: (MessageProducer, ActivationMessage, InvokerInstanceId) => Future[RecordMetadata], |
| 66 | + monitor: Option[ActorRef]): ActorRef = |
| 67 | + TestProbe().testActor |
| 68 | + } |
| 69 | + |
| 70 | + def mockMessaging(): MessagingProvider = { |
| 71 | + val messaging = stub[MessagingProvider] |
| 72 | + val producer = stub[MessageProducer] |
| 73 | + val consumer = stub[MessageConsumer] |
| 74 | + (messaging |
| 75 | + .getProducer(_: WhiskConfig, _: Option[ByteSize])(_: Logging, _: ActorSystem)) |
| 76 | + .when(*, *, *, *) |
| 77 | + .returns(producer) |
| 78 | + (messaging |
| 79 | + .getConsumer(_: WhiskConfig, _: String, _: String, _: Int, _: FiniteDuration)(_: Logging, _: ActorSystem)) |
| 80 | + .when(*, *, *, *, *, *, *) |
| 81 | + .returns(consumer) |
| 82 | + (producer |
| 83 | + .send(_: String, _: Message, _: Int)) |
| 84 | + .when(*, *, *) |
| 85 | + .returns(Future.successful(new RecordMetadata(new TopicPartition("fake", 0), 0, 0, 0l, 0l, 0, 0))) |
| 86 | + |
| 87 | + messaging |
| 88 | + } |
| 89 | + |
| 90 | + it should "execute correct LoadBalancer for the default activation strategy" in { |
| 91 | + behave like asssertActivation( |
| 92 | + ActivationStrategy("org.apache.openwhisk.core.loadBalancer.test.MockLoadBalancerDefault", Map()), |
| 93 | + Parameters(), |
| 94 | + Left(ActivationId("default-mockLoadBalancerId0"))) |
| 95 | + } |
| 96 | + |
| 97 | + it should "execute correct LoadBalancer for the custom activation strategy" in { |
| 98 | + behave like asssertActivation( |
| 99 | + ActivationStrategy( |
| 100 | + "org.apache.openwhisk.core.loadBalancer.test.MockLoadBalancerDefault", |
| 101 | + Map( |
| 102 | + "customLBStrategy01" -> StrategyConfig( |
| 103 | + "org.apache.openwhisk.core.loadBalancer.test.MockLoadBalancerCustom"))), |
| 104 | + Parameters("activationStrategy", "customLBStrategy01"), |
| 105 | + Left(ActivationId("custom-mockLoadBalancerId0"))) |
| 106 | + } |
| 107 | + |
| 108 | + def asssertActivation(activationStrategy: ActivationStrategy, |
| 109 | + annotations: Parameters, |
| 110 | + expected: Either[ActivationId, WhiskActivation]) = { |
| 111 | + val slots = 10 |
| 112 | + val memoryPerSlot = MemoryLimit.MIN_MEMORY |
| 113 | + val memory = memoryPerSlot * slots |
| 114 | + val config = new WhiskConfig(ExecManifest.requiredProperties) |
| 115 | + implicit val materializer: ActorMaterializer = ActorMaterializer() |
| 116 | + |
| 117 | + val balancer: LoadBalancer = |
| 118 | + new MuxBalancer(config, feedProbe, ControllerInstanceId("0"), mockMessaging, lbConfig(activationStrategy)) |
| 119 | + val namespace = EntityPath("testspace") |
| 120 | + val name = EntityName("testname") |
| 121 | + val invocationNamespace = EntityName("invocationSpace") |
| 122 | + val concurrency = 5 |
| 123 | + val actionMem = 256.MB |
| 124 | + val uuid = UUID() |
| 125 | + val aid = ActivationId.generate() |
| 126 | + val actionMetaData = |
| 127 | + WhiskActionMetaData( |
| 128 | + namespace, |
| 129 | + name, |
| 130 | + js10MetaData(Some("jsMain"), false), |
| 131 | + limits = actionLimits(actionMem, concurrency), |
| 132 | + annotations = annotations) |
| 133 | + |
| 134 | + val msg = ActivationMessage( |
| 135 | + TransactionId.testing, |
| 136 | + actionMetaData.fullyQualifiedName(true), |
| 137 | + actionMetaData.rev, |
| 138 | + Identity(Subject(), Namespace(invocationNamespace, uuid), BasicAuthenticationAuthKey(uuid, Secret())), |
| 139 | + aid, |
| 140 | + ControllerInstanceId("0"), |
| 141 | + blocking = false, |
| 142 | + content = None, |
| 143 | + initArgs = Set.empty, |
| 144 | + lockedArgs = Map.empty) |
| 145 | + |
| 146 | + val activation = balancer.publish(actionMetaData.toExecutableWhiskAction.get, msg) |
| 147 | + Await.ready(activation, 10.seconds) |
| 148 | + activation.onComplete(result => { |
| 149 | + result.get.onComplete(activation => { |
| 150 | + activation.get shouldBe expected |
| 151 | + }) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments