-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48754] Adding tests, structure, best practices to productioniz…
…e code base ### What changes were proposed in this pull request? This change contains several improvements that all aim to increase the code quality of the spark-connect-go repo. All in all, these changes push the repo much closer to best practice Go without major semantic changes. The changes fall in these categories: - Improve unit test coverage by about 30 percentage points - Decoupled the components in the sql package to make them individually testable and only depend on each others interfaces rather than implementation - Added context propagation to the code base. This allows users of the library to set connection timeouts, auth headers etc. - Added method/function level comments where they were missing for public functions - Removed the global var builder 'entry point' and replaced it by a normal constructor so that each builder is simply new instead of the previous copy semantics - Added a simple error hierarchy so that errors can be handled by looking at error types instead of just string values - Created constructors with required params for all structs instead of having the users create structs internally - Removed a strange case of panic'ing the the whole process if some input was invalid - Updated documentation and examples to reflect these changes ### Why are the changes needed? These changes aim (along with subsequent changes) to get this code base to a point where it will eventually be fit for production use, something that is strictly forbidden right now ### Does this PR introduce _any_ user-facing change? The PR as much as possible aims to not change the API but in a few cases this has not been possible. In particular, functions that eventually result in an outbound call to GRPC now take a context parameter. This is necessary and required for real production grade code. In addition, the builder is instantiated slightly differently (actually instantiated instead of being a global var) but the API for it otherwise remains. ### How was this patch tested? All the code that was touch, has gotten some degree of unit testing that at least ensures coverage as well as checking of output Closes #20 from mathiasschw-db/mathiasschw-db/productionize. Authored-by: Mathias Schwarz <165780420+mathiasschw-db@users.noreply.github.com> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
- Loading branch information
1 parent
884ae1c
commit 9e254ba
Showing
25 changed files
with
814 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package channel | ||
|
||
// ChannelBuilder re-exports Builder as its previous name for compatibility. | ||
// | ||
// Deprecated: use Builder instead. | ||
type ChannelBuilder = Builder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sparkerrors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type wrappedError struct { | ||
errorType error | ||
cause error | ||
} | ||
|
||
func (w *wrappedError) Unwrap() []error { | ||
return []error{w.errorType, w.cause} | ||
} | ||
|
||
func (w *wrappedError) Error() string { | ||
return fmt.Sprintf("%s: %s", w.errorType, w.cause) | ||
} | ||
|
||
// WithType wraps an error with a type that can later be checked using `errors.Is` | ||
func WithType(err error, errType errorType) error { | ||
return &wrappedError{cause: err, errorType: errType} | ||
} | ||
|
||
type errorType error | ||
|
||
var ( | ||
ConnectionError = errorType(errors.New("connection error")) | ||
ReadError = errorType(errors.New("read error")) | ||
ExecutionError = errorType(errors.New("execution error")) | ||
InvalidInputError = errorType(errors.New("invalid input")) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sparkerrors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWithTypeGivesAndErrorThatIsOfThatType(t *testing.T) { | ||
err := WithType(assert.AnError, ConnectionError) | ||
assert.ErrorIs(t, err, ConnectionError) | ||
} | ||
|
||
func TestErrorStringContainsErrorType(t *testing.T) { | ||
err := WithType(assert.AnError, ConnectionError) | ||
assert.Contains(t, err.Error(), ConnectionError.Error()) | ||
} |
Oops, something went wrong.