Skip to content

Commit

Permalink
Dequeue when message full with current paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 31, 2024
1 parent 687d9f6 commit 09961d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
32 changes: 32 additions & 0 deletions ButterSTT.Tests/MessageSystem/MessageQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,38 @@ public void PageRealtimeHardWordPersistTest()
Assert.Equal("that is just now finished!", curMessage);
}

[Fact()]
public void PageRealtimeWordTest()
{
var queue = new MessageQueue
{
MessageLength = 26,
DequeueSystem = DequeueSystems.Pagination,
RealtimeQueuePadding = 0,
WordTime = TimeSpan.Zero,
HardWordTime = TimeSpan.MaxValue
};

var firstMessage = "Testing the queue system.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(firstMessage);
queue.FinishCurrentParagraph();
var curMessage = queue.GetCurrentMessage();
_output.WriteLine($"First message: \"{curMessage}\"");
Assert.Equal(firstMessage, curMessage);

var secondMessage = "Second test.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(secondMessage);
curMessage = queue.GetCurrentMessage();
_output.WriteLine($"Second message: \"{curMessage}\"");
Assert.Equal($"-system. {secondMessage}", curMessage);

var thirdMessage = "Third test.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(thirdMessage);
curMessage = queue.GetCurrentMessage();
_output.WriteLine($"Second message expired: \"{curMessage}\"");
Assert.Equal($"-system. {thirdMessage}", curMessage);
}

[Theory()]
[InlineData(DequeueSystems.Scrolling)]
[InlineData(DequeueSystems.Pagination)]
Expand Down
8 changes: 5 additions & 3 deletions ButterSTT/MessageSystem/MessageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,18 @@ private void ExpireWords()
}

private bool IsMessageFull =>
_wordQueue.TryPeek(out var word) && _curMessageLength + word.Length > MessageLength;
(_wordQueue.TryPeek(out var word) && word.Length > AvailableMessageLength)
|| ParagraphWordEnumerator().Select(w => w.Length).FirstOrDefault(0)
> AvailableMessageLength;

private void PaginateWords()
{
var lastWord = _messageWordQueue.Last();
var hardExpired = DateTime.UtcNow >= lastWord.HardExpiryTime;
// Wait until the last word on the full page is expired
if (
(!hardExpired || _hadTextLast)
&& (!IsMessageFull || DateTime.UtcNow < lastWord.ExpiryTime)
_hadTextLast
|| (!hardExpired && (DateTime.UtcNow < lastWord.ExpiryTime || !IsMessageFull))
)
return;

Expand Down

0 comments on commit 09961d9

Please sign in to comment.