Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
c295204b
Commit
c295204b
authored
Apr 01, 2018
by
Liuhaoyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add AtomicInteger & tests
parent
30cca08f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
204 additions
and
6 deletions
+204
-6
AtomicInteger.cs
src/SkyWalking.Core/Utils/AtomicInteger.cs
+134
-0
SkyWalking.Core.Tests.csproj
test/SkyWalking.Core.Tests/SkyWalking.Core.Tests.csproj
+6
-6
AtomicIntegerTests.cs
test/SkyWalking.Core.Tests/Utils/AtomicIntegerTests.cs
+64
-0
No files found.
src/SkyWalking.Core/Utils/AtomicInteger.cs
0 → 100644
View file @
c295204b
/*
* 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.
*
*/
using
System.Threading
;
namespace
SkyWalking.Utils
{
public
class
AtomicInteger
{
private
int
_value
;
public
int
Value
=>
_value
;
public
AtomicInteger
()
:
this
(
0
)
{
}
public
AtomicInteger
(
int
defaultValue
)
{
_value
=
defaultValue
;
}
public
int
Increment
()
{
Interlocked
.
Increment
(
ref
_value
);
return
_value
;
}
public
int
Decrement
()
{
Interlocked
.
Decrement
(
ref
_value
);
return
_value
;
}
public
int
Add
(
int
value
)
{
AddInternal
(
value
);
return
_value
;
}
private
void
AddInternal
(
int
value
)
{
Interlocked
.
Add
(
ref
_value
,
value
);
}
public
override
bool
Equals
(
object
obj
)
{
switch
(
obj
)
{
case
AtomicInteger
atomicInteger
:
return
atomicInteger
.
_value
==
_value
;
case
int
value
:
return
value
==
_value
;
default
:
return
false
;
}
}
public
override
int
GetHashCode
()
{
return
_value
.
GetHashCode
();
}
public
static
AtomicInteger
operator
+(
AtomicInteger
atomicInteger
,
int
value
)
{
atomicInteger
.
AddInternal
(
value
);
return
atomicInteger
;
}
public
static
AtomicInteger
operator
+(
int
value
,
AtomicInteger
atomicInteger
)
{
atomicInteger
.
AddInternal
(
value
);
return
atomicInteger
;
}
public
static
AtomicInteger
operator
-(
AtomicInteger
atomicInteger
,
int
value
)
{
atomicInteger
.
AddInternal
(-
value
);
return
atomicInteger
;
}
public
static
AtomicInteger
operator
-(
int
value
,
AtomicInteger
atomicInteger
)
{
atomicInteger
.
AddInternal
(-
value
);
return
atomicInteger
;
}
public
static
implicit
operator
AtomicInteger
(
int
value
)
{
return
new
AtomicInteger
(
value
);
}
public
static
implicit
operator
int
(
AtomicInteger
atomicInteger
)
{
return
atomicInteger
.
_value
;
}
public
static
bool
operator
==(
AtomicInteger
atomicInteger
,
int
value
)
{
return
atomicInteger
.
_value
==
value
;
}
public
static
bool
operator
!=(
AtomicInteger
atomicInteger
,
int
value
)
{
return
!(
atomicInteger
==
value
);
}
public
static
bool
operator
==(
int
value
,
AtomicInteger
atomicInteger
)
{
return
atomicInteger
.
_value
==
value
;
}
public
static
bool
operator
!=(
int
value
,
AtomicInteger
atomicInteger
)
{
return
!(
value
==
atomicInteger
);
}
}
}
\ No newline at end of file
test/SkyWalking.Core.Tests/SkyWalking.Core.Tests.csproj
View file @
c295204b
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>
<ItemGroup>
<ProjectReference Include="..\..\src\SkyWalking.Abstractions\SkyWalking.Abstractions.csproj" />
<ProjectReference Include="..\..\src\SkyWalking.Core\SkyWalking.Core.csproj" />
</ItemGroup>
</Project>
\ No newline at end of file
test/SkyWalking.Core.Tests/Utils/AtomicIntegerTests.cs
0 → 100644
View file @
c295204b
using
System
;
using
SkyWalking.Utils
;
using
Xunit
;
namespace
SkyWalking.Core.Tests.Utils
{
public
class
AtomicIntegerTests
{
[
Fact
]
public
void
Add_Test
()
{
var
atomicInteger
=
new
AtomicInteger
();
var
result
=
atomicInteger
.
Add
(
2
);
Assert
.
Equal
(
result
,
2
);
}
[
Fact
]
public
void
Increment_Test
()
{
var
atomicInteger
=
new
AtomicInteger
(
5
);
var
result
=
atomicInteger
.
Increment
();
Assert
.
Equal
(
result
,
6
);
}
[
Fact
]
public
void
Decrement_Test
()
{
var
atomicInteger
=
new
AtomicInteger
(
5
);
var
result
=
atomicInteger
.
Decrement
();
Assert
.
Equal
(
result
,
4
);
}
[
Fact
]
public
void
Operator_Add_Test
()
{
var
atomicInteger
=
new
AtomicInteger
(
5
);
var
result
=
atomicInteger
+
2
;
Assert
.
Equal
<
int
>(
result
,
7
);
}
[
Fact
]
public
void
Operator_Sub_Test
()
{
var
atomicInteger
=
new
AtomicInteger
(
5
);
var
result
=
atomicInteger
-
2
;
Assert
.
Equal
<
int
>(
result
,
3
);
}
[
Fact
]
public
void
Equals_Test
()
{
AtomicInteger
atomicInteger
=
5
;
Assert
.
True
(
atomicInteger
.
Equals
(
5
));
}
[
Fact
]
public
void
Operator_Equals_Test
()
{
AtomicInteger
atomicInteger
=
5
;
Assert
.
True
(
atomicInteger
==
5
);
Assert
.
False
(
atomicInteger
!=
5
);
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment