Skip to content

Commit

Permalink
fix recycling of MethodReader args for proxies. Closes #980
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryShea committed Dec 17, 2024
1 parent 718db9e commit 7fd8dac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,14 @@ private <T> T checkRecycle(T o) {
((Collection<?>) o).clear();
return o;
}
// If object is Marshallable, return the same object, else return null
if (o instanceof Map) {
((Map<?, ?>) o).clear();
return o;
}
// For objects of type AbstractMarshallableCfg, reset them to their default state.
if (o instanceof AbstractMarshallableCfg) {
((AbstractMarshallableCfg) o).reset();
}
return o instanceof Marshallable ? o : null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016-2020 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.wire;

import org.junit.After;

// Test class extending MethodReaderArgumentsRecycleTest to test behavior of method readers when using proxies
public class MethodReaderArgumentsRecycleProxyTest extends MethodReaderArgumentsRecycleTest {
@Override
public void setUp() {
// Disable proxy code generation for the duration of the tests
System.setProperty("disableReaderProxyCodegen", "true");
super.setUp();
}

@After
public void after() {
// Clear the property to re-enable proxy code generation
System.clearProperty("disableReaderProxyCodegen");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.MethodReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -77,6 +78,11 @@ public void dtoCall(RegularDTO d) {
lastArgumentRef = d;
}

@Override
public void configDtoCall(ConfigDTO d) {
lastArgumentRef = d;
}

@Override
public void wrappedListCall(ListContainingDto ld) {
lastArgumentRef = ld;
Expand Down Expand Up @@ -197,6 +203,22 @@ public void testDtoRecycled() {
verifyRecycled(first, second, writer::dtoCall);
}

@Test
public void testConfigDtoRecycled() {
ConfigDTO first = new ConfigDTO();
first.s = "f";
first.b = true;

ConfigDTO second = new ConfigDTO();
second.s = "s";
// don't set b

verifyRecycled(first, second, writer::configDtoCall);

ConfigDTO dto = (ConfigDTO) lastArgumentRef;
Assert.assertFalse(dto.b);
}

// Test to ascertain that a DTO object's list field gets recycled between calls.
@Test
public void testWrappedListRecycled() {
Expand Down Expand Up @@ -258,8 +280,8 @@ public void testWrappedListAsObjectRecycledDTO() {
"}\n", lastArgumentRef.toString());
List<?> list2 = (List<?>) ((ObjectContainingDto) lastArgumentRef).list;
assertEquals(second.list, list2);
assertSame(dto0, (MyDto) list1.get(0));
assertSame(dto1, (MyDto) list1.get(1));
assertSame(dto0, list1.get(0));
assertSame(dto1, list1.get(1));

// Ensure the reference from the first call is the same as the second.
assertSame(firstRef, lastArgumentRef);
Expand Down Expand Up @@ -322,6 +344,8 @@ interface MyInterface {

void dtoCall(RegularDTO d);

void configDtoCall(ConfigDTO d);

void wrappedListCall(ListContainingDto ld);

void wrappedObjectCall(ObjectContainingDto ld);
Expand Down Expand Up @@ -349,6 +373,11 @@ public static class RegularDTO extends SelfDescribingMarshallable {
int i;
}

public static class ConfigDTO extends AbstractMarshallableCfg {
String s;
boolean b;
}

// Definition of a DTO with a list-containing field.
public static class ListContainingDto extends SelfDescribingMarshallable {
List<Object> list;
Expand Down

0 comments on commit 7fd8dac

Please sign in to comment.