Skip to content

Commit 9da667d

Browse files
Codex generated patch for failed test under arm64.
1 parent 1d51baf commit 9da667d

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/groovy/OpenAiTest.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import static datadog.environment.OperatingSystem.isArm64
2+
13
import com.google.common.base.Charsets
24
import com.google.common.base.Strings
35
import com.openai.client.OpenAIClient
@@ -67,6 +69,15 @@ abstract class OpenAiTest extends InstrumentationSpecification {
6769
def subpath = path.substring(API_VERSION.length() + 2)
6870
def recsDir = RECORDS_DIR.resolve(subpath)
6971
def recPath = recsDir.resolve(recFile)
72+
73+
// TODO: Codex generated patch to pass under arm64, revisit later.
74+
if (!recPath.toFile().exists()) {
75+
recPath = RequestResponseRecord.findEquivalentRequestRecord(
76+
RECORDS_DIR,
77+
"POST",
78+
subpath,
79+
requestBody.getBytes(Charsets.UTF_8))
80+
}
7081
if (!recPath.toFile().exists()) {
7182
throw new RuntimeException("The record file: '" + recFile + "' is NOT found at " + RECORDS_DIR + ". Set OpenAiTest.OPENAI_TOKEN to make a real request and store the record.")
7283
} else {

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/java/RequestResponseRecord.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.openai.core.ObjectMappers;
12
import com.openai.core.http.Headers;
23
import com.openai.core.http.HttpRequest;
34
import com.openai.core.http.HttpRequestBody;
@@ -10,8 +11,10 @@
1011
import java.nio.file.Path;
1112
import java.nio.file.attribute.PosixFilePermissions;
1213
import java.security.MessageDigest;
14+
import java.util.Comparator;
1315
import java.util.List;
1416
import java.util.Map;
17+
import java.util.stream.Stream;
1518

1619
public class RequestResponseRecord {
1720
/**
@@ -33,6 +36,7 @@ public class RequestResponseRecord {
3336
private static final String END_RESPONSE_BODY = "-- end response body --";
3437
private static final String KEY_VALUE_SEP = ": ";
3538
private static final char LINE_SEP = '\n';
39+
private static final Object UNPARSEABLE = new Object();
3640

3741
public final int status;
3842
public final Map<String, String> headers;
@@ -71,6 +75,28 @@ public static boolean exists(Path recordsDir, HttpRequest request) {
7175
return filePath.toFile().exists();
7276
}
7377

78+
// TODO: Codex generated patch to pass under arm64, revisit later.
79+
public static Path findEquivalentRequestRecord(
80+
Path recordsDir, String method, String path, byte[] requestBody) {
81+
Path targetDir = recordsDir.resolve(path);
82+
if (!Files.isDirectory(targetDir)) {
83+
return null;
84+
}
85+
86+
Object requestJson = tryParseJson(new String(requestBody, StandardCharsets.UTF_8));
87+
try (Stream<Path> files = Files.list(targetDir)) {
88+
return files
89+
.filter(Files::isRegularFile)
90+
.filter(file -> file.getFileName().toString().endsWith("." + method + ".rec"))
91+
.sorted(Comparator.comparing(Path::toString))
92+
.filter(file -> requestBodiesEquivalent(requestJson, requestBody, file))
93+
.findFirst()
94+
.orElse(null);
95+
} catch (IOException e) {
96+
throw new RuntimeException(e);
97+
}
98+
}
99+
74100
private static ByteArrayOutputStream readRequestBody(HttpRequest request) {
75101
ByteArrayOutputStream requestBodyBytes = new ByteArrayOutputStream();
76102
try (HttpRequestBody requestBody = request.body()) {
@@ -189,4 +215,51 @@ public static RequestResponseRecord read(Path recFilePath) {
189215
byte[] body = bodyBuilder.toString().getBytes(StandardCharsets.UTF_8);
190216
return new RequestResponseRecord(statusCode, headers, body);
191217
}
218+
219+
// TODO: Codex generated patch to pass under arm64, revisit later.
220+
public static String readRecordedRequestBody(Path recFilePath) {
221+
StringBuilder bodyBuilder = new StringBuilder();
222+
223+
try {
224+
List<String> lines = Files.readAllLines(recFilePath, StandardCharsets.UTF_8);
225+
boolean inRequestBody = false;
226+
227+
for (String line : lines) {
228+
if (line.equals(BEGIN_REQUEST_BODY)) {
229+
inRequestBody = true;
230+
} else if (line.equals(END_REQUEST_BODY)) {
231+
inRequestBody = false;
232+
} else if (inRequestBody) {
233+
if (bodyBuilder.length() > 0) {
234+
bodyBuilder.append(LINE_SEP);
235+
}
236+
bodyBuilder.append(line);
237+
}
238+
}
239+
} catch (IOException e) {
240+
throw new RuntimeException(e);
241+
}
242+
243+
return bodyBuilder.toString();
244+
}
245+
246+
private static boolean requestBodiesEquivalent(
247+
Object requestJson, byte[] requestBody, Path file) {
248+
String recordedRequestBody = readRecordedRequestBody(file);
249+
Object recordedRequestJson = tryParseJson(recordedRequestBody);
250+
251+
if (requestJson != UNPARSEABLE && recordedRequestJson != UNPARSEABLE) {
252+
return requestJson.equals(recordedRequestJson);
253+
}
254+
255+
return recordedRequestBody.equals(new String(requestBody, StandardCharsets.UTF_8));
256+
}
257+
258+
private static Object tryParseJson(String requestBody) {
259+
try {
260+
return ObjectMappers.jsonMapper().readValue(requestBody, Object.class);
261+
} catch (Exception e) {
262+
return UNPARSEABLE;
263+
}
264+
}
192265
}

0 commit comments

Comments
 (0)