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

chanbackup: fix test flake in TestUpdateAndSwap #9563

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yyforyongyu
Copy link
Member

To make it easier to debug, we break the old test into smaller ones and fix a flake caused by uncleaned test dir.

Flake found in this build, which can be reproduced locally via
make flakehunter-unit pkg=chanbackup case=TestUpdateAndSwap,

 Flake hunting unit test.
scripts/unit-test-flake-hunter.sh chanbackup TestUpdateAndSwap
Test TestUpdateAndSwap passed, count: 1
...
Test TestUpdateAndSwap passed, count: 28
Test TestUpdateAndSwap passed, count: 29
Test TestUpdateAndSwap failed. Output:
 Running debug unit tests.
GOEXPERIMENT=loopvar go test -v -tags="dev autopilotrpc chainrpc invoicesrpc neutrinorpc peersrpc routerrpc signrpc verrpc walletrpc watchtowerrpc wtclientrpc stdlog trace"  -test.run=TestUpdateAndSwap -test.timeout=30s github.com/lightningnetwork/lnd/chanbackup
=== RUN   TestUpdateAndSwap
=== PAUSE TestUpdateAndSwap
=== CONT  TestUpdateAndSwap
...
2025-02-28 02:10:31.971 [INF] CHBU: Updating backup file at /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:31.977 [INF] CHBU: Swapping old multi backup file from /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/temp-dont-use.backup to /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:31.977 [DBG] CHBU: Skipping archive of old backup file as configured
2025-02-28 02:10:31.977 [INF] CHBU: Updating backup file at /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:31.983 [INF] CHBU: Swapping old multi backup file from /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/temp-dont-use.backup to /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:31.983 [DBG] CHBU: Skipping archive of old backup file as configured
2025-02-28 02:10:31.983 [INF] CHBU: Updating backup file at /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:31.989 [INF] CHBU: Swapping old multi backup file from /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/temp-dont-use.backup to /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:31.989 [INF] CHBU: Archiving old channel backup to /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/chan-backup-archives
2025-02-28 02:10:31.996 [INF] CHBU: Updating backup file at /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:32.000 [INF] CHBU: Swapping old multi backup file from /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/temp-dont-use.backup to /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/channel.backup
2025-02-28 02:10:32.000 [INF] CHBU: Archiving old channel backup to /var/folders/n6/fbyp812n0sjfy17w5ktmwmj40000gn/T/TestUpdateAndSwap2352566219/001/chan-backup-archives
    backupfile_test.go:204: 
        	Error Trace:	.../lnd/chanbackup/backupfile_test.go:204
        	Error:      	"[- channel.backup-2025-02-28-02-10-31 - channel.backup-2025-02-28-02-10-32]" should have 1 item(s), but has 2
        	Test:       	TestUpdateAndSwap
--- FAIL: TestUpdateAndSwap (0.08s)
FAIL
FAIL	github.com/lightningnetwork/lnd/chanbackup	0.540s
FAIL
make[1]: *** [unit-debug] Error 1

@yyforyongyu yyforyongyu added flake fix no-itest no-changelog size/micro small bug fix or feature, less than 15 mins of review, less than 250 labels Feb 27, 2025
Copy link
Contributor

coderabbitai bot commented Feb 27, 2025

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

To make it easier to debug, we break the old test into smaller ones and
fix a flake caused by uncleaned test dir.
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the refactor. That's definitely nice to have!
But I think the core issue is that we use time.Now() for the file name:

timestamp := time.Now().Format("2006-01-02-15-04-05")

So whenever this test runs exactly when the system time changes from xx:xx:59 to xx:xy:00, I think we'll still have the same problem.
IMO we could use a clock.Clock in the MultiFile struct that we can then control in the unit test.

Copy link
Member Author

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a more detailed analysis in the comments @guggero. You are right the time.Now created the issue, and it's the source of the flake due to how the test was written previously - it created an uncleaned state for the following test, and the test was difficult to understand.

Unless we want to test the creation of multiple archive files, I don't think we need to touch the actual code, plus I think the flake is gone,

Test TestUpdateAndSwap passed, count: 1170
Test TestUpdateAndSwap passed, count: 1171

func TestUpdateAndSwap(t *testing.T) {
t.Parallel()

tempTestDir := t.TempDir()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, all the test cases share the same folder.


// Test with noBackupArchive set to true - should not create
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this case, we would have a file left on the disk. Since noBackupArchive is true, we won't create an archive for it.


// Test with v set to false - should create
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now enter this test, with the leftover state from the previous test, in which a file was left on the disk.


// With our backup created, we'll now attempt to swap out this
// backup, for the old one.
err = backupFile.UpdateAndSwap(PackedMulti(newPackedMulti))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a file was left on the disk from the previous test case, this call will create an archive.

archiveDir := filepath.Join(
filepath.Dir(fileName),
DefaultChanBackupArchiveDirName,
)
files, err := os.ReadDir(archiveDir)
require.NoError(t, err)
require.Len(t, files, 1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the two archives share the same filename most of the time, this would pass, otherwise it will fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flake fix no-changelog no-itest size/micro small bug fix or feature, less than 15 mins of review, less than 250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants