View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.testsuite.transport;
17  
18  import io.netty5.bootstrap.AbstractBootstrap;
19  import io.netty5.buffer.api.BufferAllocator;
20  import io.netty5.testsuite.transport.TestsuitePermutation.AllocatorConfig;
21  import io.netty5.testsuite.util.TestUtils;
22  import io.netty5.util.internal.StringUtil;
23  import io.netty5.util.internal.logging.InternalLogger;
24  import io.netty5.util.internal.logging.InternalLoggerFactory;
25  import org.junit.jupiter.api.TestInfo;
26  
27  import java.util.List;
28  
29  public abstract class AbstractComboTestsuiteTest<SB extends AbstractBootstrap<?, ?, ?>,
30          CB extends AbstractBootstrap<?, ?, ?>> {
31      protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
32      protected volatile CB cb;
33      protected volatile SB sb;
34  
35      protected abstract List<TestsuitePermutation.BootstrapComboFactory<SB, CB>> newFactories();
36  
37      protected List<AllocatorConfig> newAllocators() {
38          return TestsuitePermutation.allocator();
39      }
40  
41      protected void run(TestInfo testInfo, Runner<SB, CB> runner) throws Throwable {
42          List<TestsuitePermutation.BootstrapComboFactory<SB, CB>> combos = newFactories();
43          String methodName = TestUtils.testMethodName(testInfo);
44          for (AllocatorConfig config: newAllocators()) {
45              int i = 0;
46              for (TestsuitePermutation.BootstrapComboFactory<SB, CB> e : combos) {
47                  sb = e.newServerInstance();
48                  cb = e.newClientInstance();
49                  configure(sb, cb, config.bufferAllocator);
50                  logger.info(String.format(
51                          "Running: %s %d of %d (%s + %s) with %s",
52                          methodName, ++i, combos.size(), sb, cb,
53                          StringUtil.simpleClassName(config.bufferAllocator)));
54                  runner.run(sb, cb);
55              }
56          }
57      }
58  
59      protected abstract void configure(SB sb, CB cb, BufferAllocator bufferAllocator);
60  
61      public interface Runner<SB extends AbstractBootstrap<?, ?, ?>, CB extends AbstractBootstrap<?, ?, ?>> {
62          void run(SB sb, CB cb) throws Throwable;
63      }
64  }