-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) ### What changes were proposed in this pull request? When parsing json, handle special cases where the value might be NaN ### Why are the changes needed? Fix: #1629 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? test in real cluster and add unit test
- Loading branch information
1 parent
91191e6
commit b4c92b8
Showing
2 changed files
with
105 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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 util | ||
|
||
import ( | ||
"encoding/json" | ||
"math" | ||
"testing" | ||
) | ||
|
||
// JSONFloatWrapper is struct which contain JSONFloat | ||
type JSONFloatWrapper struct { | ||
Value1 JSONFloat `json:"value1"` | ||
Value2 JSONFloat `json:"value2"` | ||
} | ||
|
||
func TestParseJsonFloat(t *testing.T) { | ||
wrapper := JSONFloatWrapper{Value1: JSONFloat(math.NaN()), Value2: 9.99} | ||
bytes, err := json.Marshal(wrapper) | ||
if err != nil { | ||
t.Fatal("Marshal JsonFloat failed, caused by ", err.Error()) | ||
} | ||
parsed := &JSONFloatWrapper{} | ||
if err := json.Unmarshal(bytes, parsed); err != nil { | ||
t.Fatal("Unmarshal JsonFloat failed, caused by ", err.Error()) | ||
} else if !math.IsNaN(float64(parsed.Value1)) { | ||
t.Fatal("Value1 should be Nan") | ||
} else if math.Abs(float64(parsed.Value2)-9.99) > 1e-9 { | ||
t.Fatal("Value1 should be 9.99") | ||
} | ||
} | ||
|
||
func TestGetLastAppNum(t *testing.T) { | ||
jsonString := ` | ||
{ | ||
"metrics": [{ | ||
"name": "app_num_with_node", | ||
"labelNames": ["tags"], | ||
"labelValues": ["ss_v5,GRPC"], | ||
"value": 10.0, | ||
"timestampMs": null | ||
}, { | ||
"name": "total_remove_resource_time", | ||
"labelNames": ["quantile"], | ||
"labelValues": ["0.5"], | ||
"value": "NaN", | ||
"timestampMs": null | ||
}], | ||
"timeStamp": 1712575271639 | ||
} | ||
` | ||
if num, err := getLastAppNum([]byte(jsonString)); err != nil { | ||
t.Fatal("getLastAppNum failed, casued by ", err.Error()) | ||
} else if num != 10 { | ||
t.Fatal("Get wrong app number: ", num) | ||
} | ||
} |