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

simple refactor -- add tests and queue #1201

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/echarts-en.min.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<!--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.-->
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Dubbo Admin</title><link href=/admin/OpenSans.css rel=stylesheet type=text/css><link rel="/admin/shortcut icon" href=dubbo.ico type=image/x-icon><script src=/admin/echarts-en.min.js></script><link href=/admin/static/css/app.4de6ac6b.css rel=preload as=style><link href=/admin/static/css/chunk-vendors.52f99f04.css rel=preload as=style><link href=/admin/static/js/app.29227e12.js rel=preload as=script><link href=/admin/static/js/braceBase.39ce0e20.js rel=preload as=script><link href=/admin/static/js/chunk-vendors.491fd433.js rel=preload as=script><link href=/admin/static/css/chunk-vendors.52f99f04.css rel=stylesheet><link href=/admin/static/css/app.4de6ac6b.css rel=stylesheet></head><body><div id=app></div><script src=/admin/static/js/braceBase.39ce0e20.js></script><script src=/admin/static/js/chunk-vendors.491fd433.js></script><script src=/admin/static/js/app.29227e12.js></script></body></html>
14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/css/app.4de6ac6b.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/css/chunk-vendors.52f99f04.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/js/app.29227e12.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/js/app.29227e12.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/js/braceBase.39ce0e20.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/js/braceBase.39ce0e20.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/js/chunk-vendors.491fd433.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dubbo-ui/dist/static/js/chunk-vendors.491fd433.js.map

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions pkg/core/cert/setup.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// 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
//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.
//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 cert

Expand Down
95 changes: 95 additions & 0 deletions pkg/core/queue/instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 queue

import (
"sync"
"time"

"github.com/apache/dubbo-admin/pkg/core/logger"
)

// Task to be performed.
type Task func() error

// Instance of work tickets processed using a rate-limiting loop
type Instance interface {
// Push a task.
Push(task Task)
// Run the loop until a signal on the channel
Run(<-chan struct{})
}

type queueImpl struct {
delay time.Duration
tasks []Task
cond *sync.Cond
closing bool
}

// NewQueue instantiates a queue with a processing function
func NewQueue(errorDelay time.Duration) Instance {
return &queueImpl{
delay: errorDelay,
tasks: make([]Task, 0),
closing: false,
cond: sync.NewCond(&sync.Mutex{}),
}
}

func (q *queueImpl) Push(item Task) {
q.cond.L.Lock()
defer q.cond.L.Unlock()
if !q.closing {
q.tasks = append(q.tasks, item)
}
q.cond.Signal()
}

func (q *queueImpl) Run(stop <-chan struct{}) {
go func() {
<-stop
q.cond.L.Lock()
q.closing = true
q.cond.L.Unlock()
}()

for {
q.cond.L.Lock()
for !q.closing && len(q.tasks) == 0 {
q.cond.Wait()
}

if len(q.tasks) == 0 {
q.cond.L.Unlock()
// We must be shutting down.
return
}

var task Task
task, q.tasks = q.tasks[0], q.tasks[1:]
q.cond.L.Unlock()

if err := task(); err != nil {
logger.Sugar().Infof("Work item handle failed (%v), retry after delay %v", err, q.delay)
time.AfterFunc(q.delay, func() {
q.Push(task)
})
}
}
}
91 changes: 91 additions & 0 deletions pkg/core/queue/instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 queue

import (
"errors"
"sync"
"testing"
"time"
)

func TestOrdering(t *testing.T) {
numValues := 1000

q := NewQueue(1 * time.Microsecond)
stop := make(chan struct{})
defer close(stop)

wg := sync.WaitGroup{}
wg.Add(numValues)
mu := sync.Mutex{}
out := make([]int, 0)
for i := 0; i < numValues; i++ {
i := i

q.Push(func() error {
mu.Lock()
out = append(out, i)
defer mu.Unlock()
wg.Done()
return nil
})

// Start the queue at the halfway point.
if i == numValues/2 {
go q.Run(stop)
}
}

// wait for all task processed
wg.Wait()

if len(out) != numValues {
t.Fatalf("expected output array length %d to equal %d", len(out), numValues)
}

for i := 0; i < numValues; i++ {
if i != out[i] {
t.Fatalf("expected out[%d] %v to equal %v", i, out[i], i)
}
}
}

func TestRetry(t *testing.T) {
q := NewQueue(1 * time.Microsecond)
stop := make(chan struct{})
defer close(stop)

// Push a task that fails the first time and retries.
wg := sync.WaitGroup{}
wg.Add(2)
failed := false
q.Push(func() error {
defer wg.Done()
if failed {
return nil
}
failed = true
return errors.New("fake error")
})

go q.Run(stop)

// wait for the task to run twice.
wg.Wait()
}
66 changes: 66 additions & 0 deletions pkg/rule/crd/authentication/definition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 authentication

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCopy(t *testing.T) {
policy := &Policy{
Name: "test-policy",
Spec: &PolicySpec{
Action: "",
Selector: []*Selector{
{
Namespaces: []string{"test-namespace"},
NotNamespaces: []string{"test-not-namespace"},
IpBlocks: []string{"test-ip-block"},
NotIpBlocks: []string{"test-not-ip-block"},
Principals: []string{"test-principal"},
NotPrincipals: []string{"test-not-principal"},
Extends: []*Extend{
{
Key: "test-key",
Value: "test-value",
},
},
NotExtends: []*Extend{
{
Key: "test-not-key",
Value: "test-not-value",
},
},
},
},
PortLevel: []*PortLevel{
{
Port: 1314520,
Action: "test-action",
},
},
},
}

toClient := policy.CopyToClient()

assert.Equal(t, policy.Name, toClient.Name)
assert.Equal(t, policy.Spec.Action, toClient.Spec.Action)

assert.Equal(t, policy.Spec.PortLevel[0].Port, toClient.Spec.PortLevel[0].Port)
assert.Equal(t, policy.Spec.PortLevel[0].Action, toClient.Spec.PortLevel[0].Action)
}
52 changes: 52 additions & 0 deletions pkg/rule/crd/conditionroute/definition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 conditionroute

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCopy(t *testing.T) {
policy := &Policy{
Name: "test-policy",
Spec: &PolicySpec{
Priority: 0,
Enabled: true,
Force: true,
Runtime: true,
Key: "org.apache.dubbo.samples.CommentService",
Scope: "service",
Conditions: []string{
"method=getComment => region=Hangzhou",
},
ConfigVersion: "v3.0",
},
}

toClient := policy.CopyToClient()

assert.Equal(t, policy.Name, toClient.Name)

assert.Equal(t, policy.Spec.Priority, toClient.Spec.Priority)
assert.Equal(t, policy.Spec.Enabled, toClient.Spec.Enabled)
assert.Equal(t, policy.Spec.Force, toClient.Spec.Force)
assert.Equal(t, policy.Spec.Runtime, toClient.Spec.Runtime)
assert.Equal(t, policy.Spec.Key, toClient.Spec.Key)
assert.Equal(t, policy.Spec.Scope, toClient.Spec.Scope)
assert.Equal(t, policy.Spec.Conditions[0], toClient.Spec.Conditions[0])
assert.Equal(t, policy.Spec.ConfigVersion, toClient.Spec.ConfigVersion)
}
Loading