• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / servicecomb-kie / 712

28 Dec 2020 - 13:08 coverage: 59.642% (-2.1%) from 61.776%
712

Pull #168

travis-ci

web-flow
Merge 591fdf42c into 9d4a4e07f
Pull Request #168: #79 消息轨迹推送跟踪

8 of 12 new or added lines in 2 files covered. (66.67%)

2 existing lines in 1 file now uncovered.

1033 of 1732 relevant lines covered (59.64%)

3.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.45
/server/pubsub/struct.go
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
package pubsub
19

20
import (
21
        "encoding/json"
22
        "errors"
23
        "strings"
24

25
        "github.com/apache/servicecomb-kie/pkg/common"
26
        "github.com/apache/servicecomb-kie/pkg/stringutil"
27
        "github.com/apache/servicecomb-kie/pkg/util"
28
)
29

30
// const
31
const (
32
        ActionPut    = "put"
33
        ActionDelete = "delete"
34
)
35

36
//KVChangeEvent is event between kie nodes, and broadcast by serf
37
type KVChangeEvent struct {
38
        Key      string
39
        Action   string //include: put,delete
40
        Labels   map[string]string
41
        DomainID string
42
        Project  string
43
}
44

45
//NewKVChangeEvent create a struct base on event payload
46
func NewKVChangeEvent(payload []byte) (*KVChangeEvent, error) {
1×
47
        ke := &KVChangeEvent{}
1×
48
        err := json.Unmarshal(payload, ke)
1×
49
        return ke, err
1×
50
}
1×
51

52
//Topic can be subscribe
53
type Topic struct {
54
        Key          string            `json:"key,omitempty"`
55
        Labels       map[string]string `json:"-"`
56
        LabelsFormat string            `json:"labels,omitempty"`
57
        DomainID     string            `json:"domainID,omitempty"`
58
        Project      string            `json:"project,omitempty"`
59
        MatchType    string            `json:"match,omitempty"`
60
}
61

62
//ParseTopicString parse topic string to topic struct
63
func ParseTopicString(s string) (*Topic, error) {
2×
64
        t := &Topic{
2×
65
                Labels: make(map[string]string),
2×
66
        }
2×
67
        err := json.Unmarshal([]byte(s), t)
2×
68
        if err != nil {
2×
69
                return nil, err
!
70
        }
!
71
        if t.LabelsFormat == stringutil.LabelNone {
2×
72
                return t, nil
!
73
        }
!
74
        ls := strings.Split(t.LabelsFormat, "::")
2×
75
        if len(ls) != 0 {
4×
76
                for _, l := range ls {
6×
77
                        s := strings.Split(l, "=")
4×
78
                        if len(s) != 2 {
4×
79
                                return nil, errors.New("invalid label:" + l)
!
80
                        }
!
81
                        t.Labels[s[0]] = s[1]
4×
82
                }
83
        }
84
        return t, err
2×
85
}
86

87
//Match compare event with topic
88
//If the match type is set to exact in long pulling request, only update request with exactly
89
//the same label of pulling request will match the request and will trigger an immediate return.
90
//
91
//If the match type is not set, it will be matched when pulling request labels is equal to
92
//update request labels or a subset of it.
93
func (t *Topic) Match(event *KVChangeEvent) bool {
1×
94
        match := false
1×
95
        if t.Key != "" {
2×
96
                if t.Key == event.Key {
2×
97
                        match = true
1×
98
                }
1×
99
        }
100
        if t.MatchType == common.PatternExact {
1×
101
                if !util.IsEquivalentLabel(t.Labels, event.Labels) {
!
102
                        return false
!
103
                }
!
104
        }
105
        if len(t.Labels) == 0 {
1×
106
                return true
!
107
        }
!
108
        for k, v := range t.Labels {
3×
109
                if event.Labels[k] != v {
2×
UNCOV
110
                        return false
!
UNCOV
111
                }
!
112
                match = true
2×
113
        }
114
        return match
1×
115
}
116

117
//Observer represents a client polling request
118
type Observer struct {
119
        UUID      string
120
        RemoteIP  string
121
        UserAgent string
122
        Event     chan *KVChangeEvent
123
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc