|
| 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 | +package com.alipay.sofa.common.log; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | +import org.slf4j.Logger; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 26 | +import java.util.concurrent.CountDownLatch; |
| 27 | +import java.util.concurrent.LinkedBlockingQueue; |
| 28 | +import java.util.concurrent.ThreadPoolExecutor; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author huzijie |
| 33 | + * @version LoggerSpaceConcurrencyTest.java, v 0.1 2023年11月07日 3:57 PM huzijie Exp $ |
| 34 | + */ |
| 35 | +public class LoggerSpaceConcurrencyTest { |
| 36 | + private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor( |
| 37 | + 5, 50, 60, TimeUnit.SECONDS, |
| 38 | + new LinkedBlockingQueue<>(1000)); |
| 39 | + |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testLogUtilGetLoggerConcurrently() throws InterruptedException { |
| 43 | + List<Logger> loggers = new CopyOnWriteArrayList<>(); |
| 44 | + |
| 45 | + CountDownLatch countDownLatch = new CountDownLatch(10); |
| 46 | + for (int i = 0; i < 10; i++) { |
| 47 | + EXECUTOR.submit(() -> { |
| 48 | + try { |
| 49 | + Logger logger = getLogger(); |
| 50 | + loggers.add(logger); |
| 51 | + } catch (Throwable t) { |
| 52 | + t.printStackTrace(); |
| 53 | + } finally { |
| 54 | + countDownLatch.countDown(); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + Assert.assertTrue(countDownLatch.await(10, TimeUnit.SECONDS)); |
| 60 | + Assert.assertEquals(loggers.size(), 10); |
| 61 | + |
| 62 | + for (Logger logger : loggers) { |
| 63 | + Assert.assertNotEquals("Constants.DEFAULT_LOG is not expected", logger, Constants.DEFAULT_LOG); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private Logger getLogger() { |
| 69 | + return LoggerSpaceManager.getLoggerBySpace("TestLogger", "com.alipay.sofa.concurrency"); |
| 70 | + } |
| 71 | +} |
0 commit comments