forked from apache/dubbo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4e1015
commit 8bba410
Showing
7 changed files
with
347 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
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 cluster_impl | ||
|
||
|
||
|
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,121 @@ | ||
/* | ||
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 cluster_impl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/apache/dubbo-go/common/logger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
import ( | ||
perrors "github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/cluster/directory" | ||
"github.com/apache/dubbo-go/cluster/loadbalance" | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/protocol" | ||
"github.com/apache/dubbo-go/protocol/invocation" | ||
) | ||
|
||
///////////////////////////// | ||
// mock invoker | ||
///////////////////////////// | ||
|
||
var returnsuccess bool | ||
|
||
type FailsafeMockInvoker struct { | ||
url common.URL | ||
available bool | ||
destroyed bool | ||
} | ||
|
||
func NewFailsafeMockInvoker(url common.URL) *FailsafeMockInvoker { | ||
return &FailsafeMockInvoker{ | ||
url: url, | ||
available: true, | ||
destroyed: false, | ||
} | ||
} | ||
|
||
func (bi *FailsafeMockInvoker) GetUrl() common.URL { | ||
return bi.url | ||
} | ||
|
||
func (bi *FailsafeMockInvoker) IsAvailable() bool { | ||
return bi.available | ||
} | ||
|
||
func (bi *FailsafeMockInvoker) IsDestroyed() bool { | ||
return bi.destroyed | ||
} | ||
|
||
func (bi *FailsafeMockInvoker) Invoke(invocation protocol.Invocation) protocol.Result { | ||
var err error | ||
if !returnsuccess { | ||
err = perrors.New("error") | ||
} | ||
result := &protocol.RPCResult{Err: err, Rest: rest{tried: 0, success: returnsuccess}} | ||
|
||
return result | ||
} | ||
|
||
func (bi *FailsafeMockInvoker) Destroy() { | ||
logger.Infof("Destroy invoker: %v", bi.GetUrl().String()) | ||
bi.destroyed = true | ||
bi.available = false | ||
} | ||
|
||
func failSafeInvoke(t *testing.T, urlParam url.Values) protocol.Result { | ||
extension.SetLoadbalance("random", loadbalance.NewRandomLoadBalance) | ||
failsafeCluster := NewFailsafeCluster() | ||
|
||
invokers := []protocol.Invoker{} | ||
for i := 0; i < 10; i++ { | ||
url, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.%v:20000/com.ikurento.user.UserProvider", i), common.WithParams(urlParam)) | ||
invokers = append(invokers, NewFailsafeMockInvoker(url)) | ||
} | ||
|
||
staticDir := directory.NewStaticDirectory(invokers) | ||
clusterInvoker := failsafeCluster.Join(staticDir) | ||
return clusterInvoker.Invoke(&invocation.RPCInvocation{}) | ||
} | ||
|
||
func Test_FailSafeInvokeSuccess(t *testing.T) { | ||
returnsuccess = true | ||
urlParams := url.Values{} | ||
result := failSafeInvoke(t, urlParams) | ||
assert.NoError(t, result.Error()) | ||
res := result.Result().(rest) | ||
assert.True(t, res.success) | ||
} | ||
|
||
func Test_FailSafeInvokeFail(t *testing.T) { | ||
returnsuccess = false | ||
urlParams := url.Values{} | ||
result := failSafeInvoke(t, urlParams) | ||
assert.NoError(t, result.Error()) | ||
assert.Nil(t, result.Result()) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.