|
| 1 | +/* |
| 2 | + * Copyright 2013-2026, Seqera Labs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package nextflow.k8s |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit |
| 20 | + |
| 21 | +import com.google.common.cache.CacheBuilder |
| 22 | +import nextflow.k8s.client.ClientConfig |
| 23 | +import nextflow.k8s.client.K8sClient |
| 24 | +import spock.lang.Specification |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> |
| 28 | + */ |
| 29 | +class K8sExecutorTest extends Specification { |
| 30 | + |
| 31 | + def 'should cache k8s client and refresh after expiration' () { |
| 32 | + given: |
| 33 | + def CONFIG = new K8sConfig( |
| 34 | + client: [server: 'http://k8s-server'], |
| 35 | + namespace: 'test-ns', |
| 36 | + serviceAccount: 'test-sa', |
| 37 | + clientRefreshInterval: '100ms' |
| 38 | + ) |
| 39 | + and: |
| 40 | + def executor = Spy(K8sExecutor) |
| 41 | + executor.getK8sConfig() >> CONFIG |
| 42 | + // use a short-lived cache for the test |
| 43 | + executor.@clientCache = CacheBuilder.newBuilder() |
| 44 | + .expireAfterWrite(100, TimeUnit.MILLISECONDS) |
| 45 | + .build() |
| 46 | + |
| 47 | + when: 'first call to getClient' |
| 48 | + def client1 = executor.getClient() |
| 49 | + then: 'a new K8sClient is created' |
| 50 | + client1 instanceof K8sClient |
| 51 | + client1.config.server == 'http://k8s-server' |
| 52 | + |
| 53 | + when: 'second call within cache interval' |
| 54 | + def client2 = executor.getClient() |
| 55 | + then: 'returns the same cached instance' |
| 56 | + client2.is(client1) |
| 57 | + |
| 58 | + when: 'call after cache expiration' |
| 59 | + sleep(150) |
| 60 | + def client3 = executor.getClient() |
| 61 | + then: 'a new K8sClient instance is created' |
| 62 | + client3 instanceof K8sClient |
| 63 | + !client3.is(client1) |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments