mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
14 hours ago 8c46e654ad4027c6c97dfccc1e410b15f3c186df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server.loggers;
 
import static org.testng.Assert.*;
 
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
 
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.server.config.server.DebugLogPublisherCfg;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.core.ServerContext;
import org.testng.annotations.Test;
 
@SuppressWarnings("javadoc")
public class DebugLogPublisherTest extends DirectoryServerTestCase
{
  /** Minimal publisher exposing nothing but the trace settings bookkeeping under test. */
  private static final class TestDebugLogPublisher extends DebugLogPublisher<DebugLogPublisherCfg>
  {
    @Override
    public void initializeLogPublisher(DebugLogPublisherCfg config, ServerContext serverContext)
    {
      // Nothing to initialize.
    }
 
    @Override
    public void trace(TraceSettings settings, String signature, String sourceLocation, String msg,
        StackTraceElement[] stackTrace)
    {
      // Not used by these tests.
    }
 
    @Override
    public void traceException(TraceSettings settings, String signature, String sourceLocation, String msg,
        Throwable ex, StackTraceElement[] stackTrace)
    {
      // Not used by these tests.
    }
 
    @Override
    public DN getDN()
    {
      return null;
    }
 
    @Override
    public void close()
    {
      // Nothing to close.
    }
  }
 
  @Test
  public void testFreshPublisherTracesNothing() throws Exception
  {
    DebugLogPublisher<?> publisher = new TestDebugLogPublisher();
 
    assertSame(publisher.getClassSettings("com.example.Foo"), TraceSettings.DISABLED);
    assertNull(publisher.getMethodSettings("com.example.Foo"));
    assertFalse(publisher.hasTraceSettings("com.example.Foo"));
    assertFalse(publisher.hasTraceSettings("com.example.Foo#bar"));
  }
 
  @Test
  public void testGlobalSettingsOnlyApplyWhileNoTargetIsDefined() throws Exception
  {
    DebugLogPublisher<?> publisher = new TestDebugLogPublisher();
    TraceSettings global = new TraceSettings();
    publisher.addTraceSettings(null, global);
 
    assertSame(publisher.getClassSettings("com.example.Foo"), global);
 
    publisher.addTraceSettings("com.example", new TraceSettings());
 
    assertSame(publisher.getClassSettings("org.other.Bar"), TraceSettings.DISABLED);
    assertSame(publisher.removeTraceSettings(null), global);
  }
 
  @Test
  public void testMostSpecificClassScopeWins() throws Exception
  {
    DebugLogPublisher<?> publisher = new TestDebugLogPublisher();
    TraceSettings packageSettings = new TraceSettings();
    TraceSettings classSettings = new TraceSettings();
    publisher.addTraceSettings("com.example", packageSettings);
    publisher.addTraceSettings("com.example.Foo", classSettings);
 
    assertSame(publisher.getClassSettings("com.example.Foo"), classSettings);
    assertSame(publisher.getClassSettings("com.example.Foo$Inner"), classSettings);
    assertSame(publisher.getClassSettings("com.example.Bar"), packageSettings);
 
    assertSame(publisher.removeTraceSettings("com.example.Foo"), classSettings);
    assertSame(publisher.getClassSettings("com.example.Foo"), packageSettings);
    assertNull(publisher.removeTraceSettings("no.such.Scope"));
  }
 
  @Test
  public void testMethodSettings() throws Exception
  {
    DebugLogPublisher<?> publisher = new TestDebugLogPublisher();
    TraceSettings barSettings = new TraceSettings();
    TraceSettings bazSettings = new TraceSettings();
    publisher.addTraceSettings("com.example.Foo#bar", barSettings);
    publisher.addTraceSettings("com.example.Foo#baz", bazSettings);
 
    Map<String, TraceSettings> methodSettings = publisher.getMethodSettings("com.example.Foo");
    assertNotNull(methodSettings);
    assertEquals(methodSettings.size(), 2);
    assertSame(methodSettings.get("bar"), barSettings);
    assertSame(methodSettings.get("baz"), bazSettings);
    assertTrue(publisher.hasTraceSettings("com.example.Foo#bar"));
    assertFalse(publisher.hasTraceSettings("com.example.Foo#unknown"));
    assertNull(publisher.removeTraceSettings("com.example.Foo#unknown"));
    assertNull(publisher.removeTraceSettings("no.such.Class#bar"));
  }
 
  @Test
  public void testRemovingLastMethodSettingsDiscardsTheEnclosingMap() throws Exception
  {
    DebugLogPublisher<?> publisher = new TestDebugLogPublisher();
    TraceSettings barSettings = new TraceSettings();
    TraceSettings bazSettings = new TraceSettings();
    publisher.addTraceSettings("com.example.Foo#bar", barSettings);
    publisher.addTraceSettings("com.example.Foo#baz", bazSettings);
 
    assertSame(publisher.removeTraceSettings("com.example.Foo#bar"), barSettings);
    assertNotNull(publisher.getMethodSettings("com.example.Foo"));
 
    assertSame(publisher.removeTraceSettings("com.example.Foo#baz"), bazSettings);
    assertNull(publisher.getMethodSettings("com.example.Foo"));
  }
 
  /**
   * The trace settings are read without any lock, so adding and removing method settings must be
   * atomic with respect to each other: dropping the map holding the settings of a class must not
   * discard a setting added concurrently for another method of the same class.
   */
  @Test
  public void testConcurrentUpdatesDoNotLoseMethodSettings() throws Exception
  {
    final DebugLogPublisher<?> publisher = new TestDebugLogPublisher();
    final int writerCount = 4;
    final int readerCount = 2;
    final int rounds = 5000;
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch finished = new CountDownLatch(writerCount + readerCount);
    final AtomicReference<Throwable> failure = new AtomicReference<>();
 
    for (int i = 0; i < writerCount; i++)
    {
      final String methodName = "method" + i;
      new Thread(() -> {
        try
        {
          start.await();
          for (int round = 0; round < rounds; round++)
          {
            TraceSettings settings = new TraceSettings();
            publisher.addTraceSettings("com.example.Hot#" + methodName, settings);
            Map<String, TraceSettings> methodSettings = publisher.getMethodSettings("com.example.Hot");
            assertNotNull(methodSettings, "the map holding " + methodName + " was discarded");
            assertSame(methodSettings.get(methodName), settings, "the settings of " + methodName + " were lost");
            publisher.removeTraceSettings("com.example.Hot#" + methodName);
          }
        }
        catch (Throwable t)
        {
          failure.compareAndSet(null, t);
        }
        finally
        {
          finished.countDown();
        }
      }).start();
    }
 
    for (int i = 0; i < readerCount; i++)
    {
      new Thread(() -> {
        try
        {
          start.await();
          for (int round = 0; round < rounds; round++)
          {
            publisher.getClassSettings("com.example.Hot$Inner");
            Map<String, TraceSettings> methodSettings = publisher.getMethodSettings("com.example.Hot");
            if (methodSettings != null)
            {
              for (TraceSettings settings : methodSettings.values())
              {
                assertNotNull(settings.getLevel());
              }
            }
            publisher.hasTraceSettings("com.example.Hot#method0");
          }
        }
        catch (Throwable t)
        {
          failure.compareAndSet(null, t);
        }
        finally
        {
          finished.countDown();
        }
      }).start();
    }
 
    start.countDown();
    finished.await();
    if (failure.get() != null)
    {
      throw new AssertionError("concurrent access to the trace settings failed", failure.get());
    }
    assertNull(publisher.getMethodSettings("com.example.Hot"));
  }
}