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

refactor: Refactored the code to make it more modular #1078

Merged
merged 22 commits into from
Nov 14, 2023

Conversation

PranshuSrivastava
Copy link
Member

@PranshuSrivastava PranshuSrivastava commented Nov 5, 2023

Related Issue

The code did not have many modules so it became difficult to use certain parts of it, and the same work needed to be repeated again and again.

Describe the changes you've made

Made the code more modular by dividing the code into two parts, the code which is supposed the same everywhere, and the code which is supposed to be different.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Copy link

sweep-ai bot commented Nov 5, 2023

Apply Sweep Rules to your PR?

  • Apply: All new business logic should have corresponding unit tests.
  • Apply: Refactor large functions to be more modular.

@@ -19,38 +19,37 @@ import (
var Emoji = "\U0001F430" + " Keploy:"

type recorder struct {
logger *zap.Logger
Logger *zap.Logger
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to export logger, since it is called internally by the exported functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted.

@@ -271,6 +298,11 @@ func filterTcsMocks(tc *models.TestCase, m []*models.Mock, logger *zap.Logger) [
}

for _, mock := range m {
if mock.Version == "api.keploy-enterprise.io/v1beta1" {
logger.Info("This mock was recorded using the the enterprise version, may not work properly with the open source version", zap.String("mock kind:", string(mock.Kind)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove the typo here, and can the info be logged once for a test-set.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -11,5 +11,9 @@ import (

type Tester interface {
Test(path string, proxyPort uint32, testReportPath string, appCmd string, testsets []string, appContainer, networkName string, Delay uint64, passThorughPorts []uint, apiTimeout uint64, noiseConfig map[string]interface{}) bool
RunTestSet(testSet, path, testReportPath, appCmd, appContainer, appNetwork string, delay uint64, pid uint32, ys platform.TestCaseDB, loadedHook *hooks.Hook, testReportfs yaml.TestReportFS, testRunChan chan string, apiTimeout uint64, ctx context.Context, noiseConfig map[string]interface{}) models.TestRunStatus
RunTestSet(testSet, path, testReportPath, appCmd, appContainer, appNetwork string, delay uint64, pid uint32, ys platform.TestCaseDB, loadedHook *hooks.Hook, testReportfs yaml.TestReportFS, testRunChan chan string, apiTimeout uint64, ctx context.Context, noiseConfig map[string]interface{})models.TestRunStatus
Copy link
Contributor

@re-Tick re-Tick Nov 7, 2023

Choose a reason for hiding this comment

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

Here, since the parameters of this functions are long. You can add an option struct for this functions. And for the required fields can use a check for null values.

Copy link
Member Author

@PranshuSrivastava PranshuSrivastava Nov 9, 2023

Choose a reason for hiding this comment

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

done

Ys platform.TestCaseDB
LoadedHooks *hooks.Hook
AbortStopHooksInterrupt chan bool
Ok bool
Copy link
Contributor

@re-Tick re-Tick Nov 7, 2023

Choose a reason for hiding this comment

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

In the changes ok is used for returning false for errors in starting app or loading hooks so, instead of boolean we can just return the error along with InitialiseTestReturn for the InitialiseTestReturn.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

AbortStopHooksForcefully bool
Ps *proxy.ProxySet
ExitCmd chan bool
Ys platform.TestCaseDB
Copy link
Contributor

Choose a reason for hiding this comment

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

Here since the fields are exported, you can use descriptive name or add comment for the field

Copy link
Member Author

Choose a reason for hiding this comment

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

Change ps, ys and dIDE

@@ -21,13 +21,13 @@ import (
var Emoji = "\U0001F430" + " Keploy:"

type mockTester struct {
logger *zap.Logger
Logger *zap.Logger
Copy link
Contributor

Choose a reason for hiding this comment

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

If logger is not used externally, it could be unexported.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -21,13 +21,13 @@ import (
var Emoji = "\U0001F430" + " Keploy:"

type mockRecorder struct {
logger *zap.Logger
Logger *zap.Logger
Copy link
Contributor

Choose a reason for hiding this comment

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

If it is not used by the external packages, this can be unexported.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -55,6 +54,13 @@ func getNextID() int64 {
return atomic.AddInt64(&idCounter, 1)
}

type DepInterface interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove interface from the name of the type

Copy link
Member Author

Choose a reason for hiding this comment

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

Modified

cmd/record.go Outdated
@@ -19,7 +19,7 @@ func NewCmdRecord(logger *zap.Logger) *Record {
recorder := record.NewRecorder(logger)
return &Record{
recorder: recorder,
logger: logger,
Logger: logger,
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is not used by the external packages, then it can be unexported.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -98,6 +98,7 @@ func (factory *Factory) GetOrCreate(connectionID structs.ConnID) *Tracker {
}

func capture(db platform.TestCaseDB, req *http.Request, resp *http.Response, logger *zap.Logger, ctx context.Context, reqTimeTest time.Time, resTimeTest time.Time) {
V1Beta1 := models.GetVersion()
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, you can use version as the variable name

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

V1Beta1 Version = Version("api.keploy.io/v1beta1")
V1Beta2 Version = Version("api.keploy.io/v1beta2")
var (
V1Beta1 = Version("api.keploy.io/v1beta1")
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a constant and to store the current version, an un-exported global scoped variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -643,7 +643,7 @@ func recordMySQLMessage(h *hooks.Hook, mysqlRequests []models.MySQLRequest, mysq
"responseOperation": responseOperation,
}
mysqlMock := &models.Mock{
Version: models.V1Beta2,
Version: models.V1Beta1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Handle for the mysql server to register it as a dependency in the proxy.

Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Copy link
Contributor

@re-Tick re-Tick left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
Signed-off-by: Pranshu Srivastava <iampranshu24@gmail.com>
@PranshuSrivastava PranshuSrivastava merged commit cf7bb6c into keploy:main Nov 14, 2023
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants