Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
package ai.onnxruntime;
 
import ai.onnxruntime.OrtSession.SessionOptions;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
 
/** Fault-injection tests for EPContext callback ownership and native thread cleanup. */
public class EpContextDataCallbackTest {
  @BeforeAll
  public static void loadTestLibrary() throws IOException {
    OnnxRuntime.init();
    boolean isLinuxAarch64 =
        "Linux".equals(System.getProperty("os.name"))
            && "aarch64".equals(System.getProperty("os.arch"));
    String libraryPath = isLinuxAarch64 ? "/linux-aarch64/" : "/";
    String libName = libraryPath + System.mapLibraryName("onnxruntime4j_jni_test");
    Assumptions.assumeTrue(
        EpContextDataCallbackTest.class.getResource(libName) != null,
        "The native test library '"
            + libName
            + "' is not on the classpath; skipping as it is not bundled in all build"
            + " configurations (e.g. packaged jar testing).");
    System.load(TestHelpers.getResourcePath(libName).toString());
  }
 
  @Test
  public void successfulReadDetachesNativeWorker() throws OrtException {
    exerciseReadCallbackAllocator(OnnxRuntime.ortApiHandle, name -> new byte[] {1, 2, 3}, 0);
  }
 
  @Test
  public void nullAllocationDetachesNativeWorker() throws OrtException {
    exerciseReadCallbackAllocator(OnnxRuntime.ortApiHandle, name -> new byte[] {1, 2, 3}, 1);
  }
 
  @Test
  public void throwingAllocationDetachesNativeWorker() throws OrtException {
    Assumptions.assumeTrue(
        supportsAllocatorExceptions(), "The native build disables C++ exceptions.");
    exerciseReadCallbackAllocator(OnnxRuntime.ortApiHandle, name -> new byte[] {1, 2, 3}, 2);
  }
 
  @Test
  public void failedHolderAllocationDoesNotInstallCallback() throws OrtException {
    try (SessionOptions options = new SessionOptions()) {
      assertHolderAllocationFailure(options);
      Assertions.assertNull(options.getEpContextDataReadCallbackRegistration());
 
      options.setEpContextDataReadCallback(name -> new byte[0], 1024);
      SessionOptions.EpContextDataReadCallbackRegistration registration =
          options.getEpContextDataReadCallbackRegistration();
      Assertions.assertEquals(1, registration.getReferenceCount());
      options.clearEpContextDataReadCallback();
      Assertions.assertEquals(0, registration.getReferenceCount());
    }
  }
 
  @Test
  public void failedHolderAllocationPreservesPreviousRegistration() throws OrtException {
    SessionOptions.EpContextDataReadCallbackRegistration registration;
    try (SessionOptions options = new SessionOptions()) {
      options.setEpContextDataReadCallback(name -> new byte[0], 1024);
      registration = options.getEpContextDataReadCallbackRegistration();
 
      assertHolderAllocationFailure(options);
      Assertions.assertSame(registration, options.getEpContextDataReadCallbackRegistration());
      Assertions.assertEquals(1, registration.getReferenceCount());
      try (SessionOptions.NativeSessionOptionsSnapshot snapshot = options.createSnapshot()) {
        Assertions.assertSame(registration, snapshot.getEpContextDataReadCallbackRegistration());
        Assertions.assertEquals(2, registration.getReferenceCount());
      }
      Assertions.assertEquals(1, registration.getReferenceCount());
    }
    Assertions.assertEquals(0, registration.getReferenceCount());
  }
 
  private static void assertHolderAllocationFailure(SessionOptions options) {
    int[] counts = new int[3];
    Assertions.assertThrows(
        OutOfMemoryError.class,
        () ->
            failReadCallbackHolderAllocation(
                OnnxRuntime.ortApiHandle, options.getNativeHandle(), name -> new byte[0], counts));
    // Global references created/released, followed by native installation attempts.
    Assertions.assertArrayEquals(new int[] {1, 1, 0}, counts);
  }
 
  private static native boolean supportsAllocatorExceptions();
 
  private static native void exerciseReadCallbackAllocator(
      long apiHandle, SessionOptions.EpContextDataReadCallback callback, int allocationMode)
      throws OrtException;
 
  private static native void failReadCallbackHolderAllocation(
      long apiHandle,
      long optionsHandle,
      SessionOptions.EpContextDataReadCallback callback,
      int[] counts)
      throws OrtException;
}