-
Notifications
You must be signed in to change notification settings - Fork 16
Add shutdown hook migrator #146
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
Conversation
WalkthroughA new migration function, Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Test_MigrateShutdownHook
participant Migrator as MigrateShutdownHook
participant File as Go Source File
Test->>File: Write file with old shutdown hook
Test->>Migrator: Call MigrateShutdownHook on directory
Migrator->>File: Read Go source file
Migrator->>File: Replace OnShutdown with OnPostShutdown and update function signature
Migrator-->>Test: Print migration confirmation
Test->>File: Verify file content updated
Test->>Test: Assert output contains migration message
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested labels
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces an automated migration to facilitate the transition from the deprecated OnShutdown hook to the new OnPostShutdown hook. The migration handles both the renaming of the hook and the necessary adjustment of inline function signatures to include an error parameter, aiming to simplify the upgrade path for users.
Highlights
- New Migration Added: A new migration function,
MigrateShutdownHook, has been introduced to automatically update deprecatedOnShutdownhooks toOnPostShutdown. - Function Signature Adaptation: The
MigrateShutdownHooknot only renames the hook but also adapts inline hook functions to accept anerrorparameter, aligning with the updatedOnPostShutdownsignature. - Migration Registration: The new
MigrateShutdownHookhas been registered in thev3migration list, ensuring it is executed as part of the standard migration process. - Comprehensive Testing: A dedicated test case (
Test_MigrateShutdownHook) has been added to verify the correct functionality of the new migrator, including its ability to transform file content and log migration messages.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds a new migrator to update OnShutdown hooks to OnPostShutdown. I've provided suggestions to improve the performance and robustness of the regular expression-based replacement, and to enhance the new test to cover more edge cases.
| func Test_MigrateShutdownHook(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mhooks") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import "github.com/gofiber/fiber/v2" | ||
| func main() { | ||
| app := fiber.New() | ||
| app.Hooks().OnShutdown(func() error { return nil }) | ||
| }`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateShutdownHook(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, `.Hooks().OnPostShutdown(func(err error) error {`) | ||
| assert.Contains(t, buf.String(), "Migrating shutdown hooks") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
cmd/internal/migrations/v3/common.go (1)
405-406: Consider edge cases for the method name replacement.The regex pattern
\.Hooks\(\)\.OnShutdown\(should work for standard usage, but consider if there are edge cases with whitespace or different formatting that might be missed.For more robust matching, consider:
- reName := regexp.MustCompile(`\.Hooks\(\)\.OnShutdown\(`) + reName := regexp.MustCompile(`\.Hooks\(\)\s*\.OnShutdown\s*\(`)cmd/internal/migrations/v3/common_test.go (1)
838-843: Consider testing additional shutdown hook patterns.While the current test covers the basic inline function case, consider adding test cases for:
- Named functions passed to OnShutdown
- Multiple OnShutdown calls in the same file
- Different formatting/whitespace variations
Example additional test case:
file := writeTempFile(t, dir, `package main import "github.com/gofiber/fiber/v2" func shutdownHandler() error { return nil } func main() { app := fiber.New() app.Hooks().OnShutdown(shutdownHandler) app.Hooks().OnShutdown(func() error { return nil }) }`)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
cmd/internal/migrations/lists.go(1 hunks)cmd/internal/migrations/v3/common.go(1 hunks)cmd/internal/migrations/v3/common_test.go(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: norri
PR: gofiber/recipes#2701
File: clean-code/app/main.go:0-0
Timestamp: 2024-11-23T19:35:36.767Z
Learning: In the Go `clean-code` example (`clean-code/app/main.go`) in the `gofiber/recipes` repository, it's acceptable to omit graceful shutdown handling, as the example code prioritizes simplicity over production-level practices.
Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the `Test_App_ShutdownWithContext` function in `app_test.go`, the `clientDone` channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional `time.Sleep` calls.
Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.
cmd/internal/migrations/v3/common_test.go (10)
Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the Test method in app.go.
Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the Test_App_ShutdownWithContext function in app_test.go, the clientDone channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional time.Sleep calls.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-02T23:03:31.727Z
Learning: Unit tests in this project use testify require.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests in this project use testify require.
Learnt from: norri
PR: gofiber/recipes#2701
File: clean-code/app/main.go:0-0
Timestamp: 2024-11-23T19:35:36.767Z
Learning: In the Go clean-code example (clean-code/app/main.go) in the gofiber/recipes repository, it's acceptable to omit graceful shutdown handling, as the example code prioritizes simplicity over production-level practices.
Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.
Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-10-08T19:06:06.583Z
Learning: The project uses the testify/assert package for assertions in unit tests.
Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-06-30T00:38:06.580Z
Learning: Parallel tests for Session.Save already exist in the middleware/session/session_test.go file, specifically in the Test_Session_Save and Test_Session_Save_Expiration functions.
Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Parallel tests for Session.Save already exist in the middleware/session/session_test.go file, specifically in the Test_Session_Save and Test_Session_Save_Expiration functions.
Learnt from: efectn
PR: gofiber/fiber#3162
File: hooks_test.go:228-228
Timestamp: 2024-12-13T08:14:22.851Z
Learning: In Go test files, prefer using the require methods from the testify package for assertions instead of manual comparisons and calls to t.Fatal or t.Fatalf.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build (1.24.x, windows-latest)
🔇 Additional comments (4)
cmd/internal/migrations/v3/common.go (2)
401-419: LGTM! Clean implementation following established patterns.The migration function correctly updates both the hook method name and inline function signatures. The regex patterns are appropriate for the typical usage patterns in Fiber applications.
408-409: Replacement logic covers all inline callback casesI searched the entire repo for
OnShutdownhooks and only found inline usages incommon_test.goof the form:
app.Hooks().OnShutdown(func() error { … })OnShutdownError: func(err error) { … }OnShutdownSuccess: func() { … }Your two‐step migration already handles all of these:
reNamerenames.Hooks().OnShutdown(→.Hooks().OnPostShutdown(reInline’s pattern\.OnPostShutdown\(func\(\s*\)matches bothfunc()andfunc( )and injectserr, yieldingfunc(err error)No other formatting variations or variable‐passed hooks were detected. No further changes needed.
cmd/internal/migrations/lists.go (1)
44-44: Perfect placement in the migration sequence.The
MigrateShutdownHookfunction is logically positioned betweenMigrateListenerCallbacksandMigrateListenMethods, which makes sense as they're all related to application lifecycle and shutdown handling.cmd/internal/migrations/v3/common_test.go (1)
831-852: Comprehensive test coverage following established patterns.The test effectively validates both the method name migration and function signature transformation. It properly uses testify assertions and follows the same structure as other migration tests.
Summary
Testing
go test ./...https://chatgpt.com/codex/tasks/task_e_68865a5ab46c8326a0c37cf1f1d4736e
Summary by CodeRabbit
New Features
Tests