Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix channel cache lock handling #2795

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update guild cache reference in GuildChannel#getGuild
  • Loading branch information
MinnDevelopment committed Jan 25, 2025
commit 63f744a5820bbd9e381f4ddf56399939917e47d5
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

package net.dv8tion.jda.internal.entities.channel.middleman;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.channel.AbstractChannelImpl;
@@ -38,6 +39,9 @@ public AbstractGuildChannelImpl(long id, GuildImpl guild)
@Override
public GuildImpl getGuild()
{
Guild cachedGuild = getJDA().getGuildById(id);
if (cachedGuild instanceof GuildImpl)
return this.guild = (GuildImpl) cachedGuild;
return guild;
}

13 changes: 11 additions & 2 deletions src/test/java/net/dv8tion/jda/test/entities/EntityManagerTest.java
Original file line number Diff line number Diff line change
@@ -33,8 +33,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

class EntityManagerTest extends IntegrationTest
{
@@ -62,6 +61,7 @@ void createTextChannelWhileInvalidatingCache()
entityBuilder.createTextChannel(guild, TestData.CHANNEL_CREATE, Constants.GUILD_ID);

GuildImpl newGuild = mock(GuildImpl.class);
when(newGuild.getJDA()).thenReturn(jda);
when(newGuild.getChannelView()).thenReturn(new SortedChannelCacheViewImpl<>(GuildChannel.class));
when(newGuild.getTextChannelById(eq(Constants.CHANNEL_ID))).then(
invocation -> newGuild.getChannelView().getElementById(Constants.CHANNEL_ID)
@@ -72,6 +72,15 @@ void createTextChannelWhileInvalidatingCache()
assertThat(newGuild.getChannelView().getElementById(Constants.CHANNEL_ID)).isNotNull();
assertThat(createdChannel)
.isSameAs(newGuild.getChannelView().getElementById(Constants.CHANNEL_ID));


reset(jda);

when(jda.getGuildById(eq(Constants.GUILD_ID))).thenReturn(newGuild);
assertThat(createdChannel.getGuild())
.isSameAs(newGuild);

verify(jda, times(1)).getGuildById(anyLong());
}

static class TestData
Loading